The Assignment =
|
We saw that, when declaring a variable, a memory space is reserved for
it. Such a space is empty until you fill it with a value. This is
performed with the assignment operation. The assignment operation gives a
value to a variable. It is performed using the = operator. Its syntax is:
VariableName = Value
The VariableName factor must be a valid variable name.
It cannot be a value such as a numeric value or a (double-quoted) string.
Here is an example that assigns a numeric value to a variable:
|
<Script Language="JavaScript">
salary = 12.55;
</Script>
|
Once a variable has been declared and assigned a value, you can call
the document.write() method to display its value. Since the
variable is part of your script and not an HTML tag, it doesn't need to be
included in double-quotes (the proper sentence is that, "it doesn't
need to be passed as a string"; but we have not learned what it means to pass
an argument). Here is an example:
|
<Script Language="JavaScript">
Salary = 12.55;
document.write(Salary);
</Script>
|
To improve, safeguard, and make your code more efficient, we have learned
that you should declare a variable before using it. This is done using the
var keyword. The above script could be written as follows:
|
<Script Language="JavaScript">
var Salary;
Salary = 12.55;
document.write(Salary);
</Script>
|
The above code declares a variable before assigning it a value. You will
usually perform this assignment when you want to change the value held by
a variable. The first time the browser encounters a variable, you can make
sure that the variable is holding a value, just as done above.
Alternatively, you can assign a value to a variable when you are declaring
it, on the same line as its declaration. The above script can be changed
as follows:
|
<Script Language="JavaScript"> var Salary = 12.55; document.write(Salary); </Script> |
Providing a starting value to a variable when it is declared is referred
to as initializing the variable.
|
Practical Learning: Using the Assignment Operator
|
|
The Addition +
|
The addition is an operation used to add one value to another. The syntax
used it:
Value1 + Value2
To perform this operation, the interpreter would add the
value of Value1 to that of Value2. You can use such an operation to
display the result on an HTML page. Here is an example:
The addition operation as implemented in JavaScript
can be applied to natural numbers called integers, to floating-point numbers
called float or double-precision numbers, or to strings. Here is an example
that illustrates this:
You can also apply this operation on variables. For
example, once a variable holds a value, you can add another value to it:
Salary + 0.55
You can also add the values of two variables
using their names:
SalaryWeek1 + SalaryWeek2
As done in the above script, you can display the
addition using the document.write() method.
After the addition operation is performed, it produces
a resulting value. Sometimes you will not simply need to display the value
on a web page: you may want to use it in another calculation. Therefore,
you can store the result in a another variable, then use the result as you
see fit.
|
Practical Learning: Performing Additions
|
|
The Subtraction -
|
The subtraction is an operation used to subtract one value from another.
The syntax used it:
Value1 - Value2
To perform this operation, the interpreter would subtract
the value of Value1 from that of Value2. You can use such an operation to
display the result on an HTML page. Here is an example:
The subtraction operation as implemented in JavaScript
can be applied to natural numbers or decimal numbers. The subtraction can also be applied on variables
and/or their values.
|
The Multiplication *
|
The multiplication is used to add a number to itself a certain number of
times. This operation is performed using the * operator. Its syntax is:
Value1 * Value2
This operation can be performed on numeric values as
follows:
The multiplication operation can be applied to values
or variables that hold valid values. After the operation is performed, it
produces a result that can be displayed on a web page or stored in a
variable for later retrieval.
|
The Division /
|
The division operation is used to divide a numeric value or the value held
by a variable. The division operation is performed using the forward
slash. Its syntax is:
Value1 / Value2
This operation can be performed on numeric values as
follows:
The division operation can be applied to values or
variables that hold valid values
|
The Negation -
|
In mathematics, a number written as 10500 or 44.12 is considered a positive
number. In the computer world, we referred to such a number as unsigned
(because it doesn't have a sign). An unsigned number has a value greater
than 0. If a number is less than 0, such a number qualifies as negative.
To indicate that a number is negative, you write the - sign on its
left. For example -32 is called a negative number.
A value or a variable is made negative by applying the
- operator to its left. Here is an example:
<Script Language="JavaScript">
var Temperature
Temperature = -32
</Script>
|
The negation operator is referred to as a unary
operator because it applies to only one value or variable. As illustrated
above, a negative number can be assigned to a variable. A variable can
also be made negative by using this operator.
No comments:
Post a Comment