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
| + | addition | a + b | 11 |
| - | subtraction | 5 - a | 2 |
| * | multiplication | 3 * b | 24 |
| / | division | 6 / a | 2 |
String Operators
In this example a is 'Hello' and b is 'World'.
| + | concatination | a + b | HelloWorld |
Shorthand
| ++ | 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 |
Comparison
| == | 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 |
Boolean logic
| && | 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.
Comments
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.