The operators
allow you to manage or change the value of variables. Just like other
programming languages, JavaScript also supports different types of operators.
The three basic types of operators are:
·
Assignment operators: The assignment operators allow you to assign
values to variables. You can also use this operator to assign a calculated
or replaced value to a variable
Example:
Operator |
English |
Example |
Result |
Explanation |
=
|
Assignment |
1)
var2=5000
var1
=var2
2)
Income = perks + salary
3)
num = count++ |
var1=
5000 |
The
statement var1 =var2 will assign the value of var2 to var1. So
the value of var1 will also be 5000.
The
statement Income = perks + salary use this operator to assign
a calculated value to a variable
The
statement num = count++ first assigns the value of count to num
and then add 1 to count.
|
·
Arithmetic operators: The arithmetic operators allow you to perform
arithmetic operations on the variables or numbers.
Example:
Operator |
English |
Example |
Result |
Explanation |
+ |
Addition |
c=a
+ b |
a=2
b=2
c=4 |
The
statement a + b will add the values of both “a” and “b”. So “c”
contains 4. |
- |
Subtraction |
c=a
- b |
a=6
b=2
c=4 |
The
statement a - b will subtract the value of both “b” from “a”.
So “c” contains 4. |
* |
Multiplication |
c=a
* b |
a=6
b=2
c=12 |
The
statement a * b will multiply the values of both “a” and “b”.
So “c” contains 12. |
/ |
Division |
c=a
/ b |
a=6
b=2
c=3 |
The
statement a / b will divide the value of “a” with “b”. So “c”
contains 3. |
% |
Modulus |
c=a
/ b |
a=53
b=10
c=3 |
The
statement a % b will divide the value of “a” with “b” and shows
the remainder. So “c” contains 3. |
++ |
PreIncrement |
a=5
b
= ++a
|
a=6
b=6 |
The
statement b = ++a first adds 1 to the value in "a" and then assigns
that value to "b" so both variables now contain 6. |
++ |
PostIncrement |
a=5
b
= a++ |
b=5
a=6 |
The
statement b = a++ first assigns the value from "a" into "b" and
then adds 1 to "a" so now "b" contains 5 and "a" contains 6. |
–– |
PreDecrement |
a=5
b
= --a
|
a=4
b=4 |
The
statement d = --a first subtracts one from "a" and then assigns
the result to "d" so both variables now contain 4. |
–– |
PostDecrement |
a=5
b = a--; |
b=5
a=4 |
The
statement b = a-- first assigns the value from "a" into "b" and
then adds 1 to "a" so now "b" contains 5 and "a" contains 4. |
- |
Negation |
b=-a |
a=20
b=-20 |
The
statement b=-a will change the positive value of “a” to negative
value in “b”. So “b” contains -20 |
·
Concatenation operator: The concatenation operator allows you to
combine two string values.
Example:
Operator |
English |
Example |
Result |
Explanation |
+ |
Concatenation |
c=”my”
+ “car”
var
= “6” + ”5” |
Mycar |
The
statement my + car will concatenate the values “my” and “car”.
So “c” will contain “mycar” and “var” will contain “65” |
·
Comparison operators: The comparison operators allow you to compare
two variables and find if they are equal, unequal, greater or less.
Example:
Consider
two values:
var1
= 2
var
2= 4
Operator |
English |
Example
|
Result |
|
== |
Equal
To |
var
1 == var 2 |
false |
The
statement var 1 == var 2 will compare the values of var1 and var2
for equality and displays false because var1 is 2 and var2 is
4 |
!= |
Not
Equal To |
var
1 != var 2 |
true |
The
statement var 1 != var 2 will compare the values of var1 and var2
for inequality and displays true because var1 is 2 and var2 is
4 |
< |
Less
Than |
var
1 < var 2 |
true |
The
statement will compare the values of var1 and var2 to check if
var1 is less than var2 and displays true because var1 is 2 and
var2 is 4 |
> |
Greater
Than |
var
1 > var 2 |
false |
The
statement will compare the values of var1 and var2 to check if
var1 is greater than var2 and displays false because var1 is 2
and var2 is 4 |
<= |
Less
Than or Equal To |
var
1 <= var 2 |
true |
The
statement will compare the values of var1 and var2 to check if
var1 is less than or equal to var2 and displays true because var1
is 2 and var2 is 4 |
>= |
Greater
Than or Equal To |
var
1 >= var 2 |
false |
The
statement will compare the values of var1 and var2 to check if
var1 is greater than or equal to var2 and displays false because
var1 is 2 and var2 is 4 |
|