JavaScript Operators
In this installment we’ll take a look at JavaScript operators, which are used
to accomplish many different tasks. This may be a review for some of you; though
others you may want to study this subject in more depth.
An operator is a tool used to manipulate data. It can perform mathematical
calculations, data comparisons, and assign data values to variables. Generally,
operators are represented by mathematical symbols but in a few cases they are
actual words. The term “operator”
is derived from the action that is performed on a piece of data. The action
is called an “operation:” the tool used to perform the operation is called an
“operator.” The values (i.e., numbers assigned to a variable) that are used
in the operation are called “operands”
(I’ll use these two terms — value and operand —
interchangeably).
The most common operators used in JavaScript can be divided into six different categories:
|
As we discuss these operators below, go ahead and try out the code using the
template that we made last time. This way you’ll see exactly what the code
is doing. Remember also to type them in yourself as that will help you when
you write your own. As we go through each example, try to figure out what is
happening before you read the explanation.
Mathematical Operators
Operator |
Name | Description |
+ | Addition | Adds two values together |
– | Subtraction |
Subtracts one value from the other |
* | Multiplication | Multiplies two values |
⁄ | Division | Divides one value by another |
% |
Modulus |
Divides one value by another and returns the remainder |
++ | Increment | Adds 1 to a value |
— | Decrement | Subtracts 1 from a value |
– | Negation | Changes the sign of the value [Makes a positive value negative and a negative value positive.] |
Addition Operator (+)
This operator is used to add together numbers or strings.
var theSum=2+5; alert(theSum);
In this script we are declaring a variable called theSum
and initializing it with a value of 2+5. Next we call an alert window to display the results of the variable. This would result in an alert window displaying the number 7. Go ahead and try it.
var myNum=2; var yourNum=5; var theSum=myNum+yourNum; alert(theSum);
This script declares and initializes three variables, myNum
,
yourNum
, and theSum
. The first two are given the values
of 2 and 5, respectively. The third variable is a calculation, adding the first
and second variables. Next, the script calls an alert window to display the
results of the third variable, theSum
. This would result in an
alert window with the number 7. This script is basically the same as the previous
one except that we can change the value of each variable individually.
var theSum=5; var newNum=theSum+3 alert(newNum);
In this last example, we declare and initialize two variables, theSum
and newNum
. The first variable is given a value of 5. In the second variable, a value of 3 is added to the value of the first variable. An alert window is then called to display the results of the second variable, newNum
, which produces the value of 8.
Subtraction Operator (-)
This operator is used to subtract the value on the right of the operator from the value on the left, i.e., “7 – 2” would read “seven minus two”.
var newNum=7; var newNum2=2; var result=newNum-newNum2; alert(result);
This script declares and initializes three variables, newNum
, newNum2
, and result
. The first two are given the value of 7 and 2, respectively. The third variable, result
, is a calculation, subtracting the variable newNum2
from the variable newNum
. An alert window is then called to display the results of the third variable, result
, which produces a value of 5.