Previous
Arrays 
Next
If Condition 
ABC of JavaScript : An Interactive JavaScript Tutorial
Operators

Operators

Note : The variable 'a' has the value 3 and 'b' has the value 8. These values will NOT change - in other words, these values will be same for every example.

Arithmetic Operators

OperatorExplanationExampleExample Result
+additiona + b11
-subtraction5 - a2
*multiplication3 * b24
/division6 / a2

String Operators

In this example a is 'Hello' and b is 'World'.
OperatorExplanationExampleExample Result
+concatinationa + bHelloWorld

Shorthand

OperatorExplanationExampleExample Result
++Adds 1 to the value on the left (so if i were equal to 1, i++ would equal 2)a++a will be 4
--Subtracts 1 from the value on the left.a--a will be
+=Adds the values to the left and right of the operator and then stores the value in the left variable. a += ba will be 11
-=Subtracts the values to the left and right of the operator and then stores the value in the left variable. b -= ab will be 5
*=Multiplies the values to the left and right of the operator and then stores the value in the left variable. a *= b;a will be 24

Comparison

OperatorExplanationExampleExample Result
==equalityif(a == b)False
!=inequalityif(a != b)True
<less thanif(a < b)True
>greater thanif(a > b)False
<=less than or equalif(a <= b)True
>=greater than or equalif(a >= b)False

Boolean logic

OperatorExplanationExample Example Result
&&AND if(a>b && b>7)False
||OR if(a>b || b>7)True
!NOT if(!b)False

Most of these operators are common among other popular languages like C++, Java etc. So if you have any programming experience, you will be well at ease with the operators in JavaScript.

Now lets put those operators to good use by looking at the Control flow structures - where operators will be used most.

Previous
Arrays 
Next
If Condition 
blog comments powered by Disqus