Previous Arrays |
ABC of JavaScript : A JavaScript Tutorial Operators |
Next If Condition |
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 OperatorsOperator | Explanation | Example | Example Result |
+ | addition | a + b | 11 |
- | subtraction | 5 - a | 2 |
* | multiplication | 3 * b | 24 |
/ | division | 6 / a | 2 |
Operator | Explanation | Example | Example 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 += b | a will be 11 |
-= | Subtracts the values to the left and right of the operator and then stores the value in the left variable. | b -= a | b 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 |
Operator | Explanation | Example | Example Result |
== | equality | if(a == b) | False |
!= | inequality | if(a != b) | True |
< | less than | if(a < b) | True |
> | greater than | if(a > b) | False |
<= | less than or equal | if(a <= b) | True |
>= | greater than or equal | if(a >= b) | False |
Operator | Explanation | Example | 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 |
Contents | Next If Condition |