Javascript Operators
Arithmetic and Assignment Operators
We will start this chapter wil the simple example below that shows the five arithmetic operators.
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
|
If you "run" the above javascript code you should see the following output
x + y is 30
x - y is 10 x * y is 200 x / y is 2 x % y is 4 |
You can try to experiment it online at the following link ( opens in a new window).
Javascript Arithmetic Operator Example
This was expected. The Modulus operator % gives the remainder. So 5 % 2 is 1, 10 % 8 is 2 and 10 % 2 is 0.
If there multiple mathematical operators, it is better to use parentheses to bring clarity to expression.
Increment and decrement operators
Increment and decrement operators and increase values by 1. Historically, they bear similarity to C language. The example below should clarify it
|
This gives out the following result
x++ is 21
y-- is 9 |
Try this example at the following link ( opens in a new window).
Javascript Increment and decrement Operator Example
You may also like to note that the increment operation
z = x++;
is slighty different from
z = ++x;
In both cases the value of x in incremented by 1. But in the case z = ++x; the value of z in assigneed AFTER the increment operation. In case of the z = x++ the value of z is assigned BEFORE the increment operation.
Assignment operators
We have already seen one assignment operator "=" . The table below lists out these assignment operators.
Operator | Example | Is equivalent to |
---|---|---|
= | x=y | x=y |
+= | x+=y | x=x+y |
-= | x-=y | x=x-y |
*= | x*=y | x=x*y |
/= | x/=y | x=x/y |
%= | x%=y | x=x%y |
Given below is an example that uses these arithmetic operators.
|
This gives out the following result
x+=y is 30
x-=y is 10 x*=y is 200 x/=y is 2 x%=y is 0 |
The assignment operator x+=y looks confusing at first. You may prefer use x=x+y if you feel comfirtable. But the programming community is notorious for brevity. So you will always find examples and scripts that uses x+=y more often than not. An easy way to remember is the x=+y is not valid. You can remember it like this
x = +y will assign +y to x which is not we wanted. So the other one x+=y obviously looks coorect.
Try this example at the following link ( opens in a new window).
Javascript Assignment Operator Example
Exercises
If strongly believe that, if you do not take some exercises, it is easy to forget the concepts. Try to practice the following examples.
1. Consider the following two code snippets
var x = 20 ;
var y = 10 ;
z = ++x + ++y;
and
var x = 20 ;
var y = 10 ;
z = ++x + ++y;
What are the values of z in the two cases ? Verify your assumptions by making the changes in the code here
2. Try to write a mathematical expression that will calculate the sum of the square of two numbers
z = x2 + y2
Verify it by running the code.
3. Look a the following two code snippets
var x = 20 ;
var y = 10 ;
x+=++y;
and
var x = 20 ;
var y = 10 ;
x+=y++;
Predict the value of x in the two cases. Verify your assumptions by making the changes in the code here