Jsref Operators
* * *
JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more.
* * *
## JavaScript Arithmetic Operators
Arithmetic operators are used to perform operations on two variables or values.
Given **y = 5**, the following table explains the use of arithmetic operators:
| Operator | Description | Example | y Value | x Value | Online Example |
| --- | --- | --- | --- | --- | --- |
| + | Addition | x = y + 2 | y = 5 | x = 7 | [Example Β»](#) |
| - | Subtraction | x = y - 2 | y = 5 | x = 3 | [Example Β»](#) |
| * | Multiplication | x = y * 2 | y = 5 | x = 10 | [Example Β»](#) |
| / | Division | x = y / 2 | y = 5 | x = 2.5 | [Example Β»](#) |
| % | Modulus (Remainder) | x = y % 2 | y = 5 | x = 1 | [Example Β»](#) |
| ++ | Increment | x = ++y | y = 6 | x = 6 | [Example Β»](#) |
| x = y++ | y = 6 | x = 5 | [Example Β»](#) |
| -- | Decrement | x = --y | y = 4 | x = 4 | [Example Β»](#) |
| x = y-- | y = 4 | x = 5 | [Example Β»](#) |
For more information on arithmetic operators, you can read our (#).
* * *
## JavaScript Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
Given **x=10** and **y=5**, the following table explains the assignment operators:
| Operator | Example | Equivalent | x Value | Online Example |
| --- | --- | --- | --- | --- |
| = | x = y | x = y | x = 5 | [Example Β»](#) |
| += | x += y | x = x + y | x = 15 | [Example Β»](#) |
| -= | x -= y | x = x - y | x = 5 | [Example Β»](#) |
| *= | x *= y | x = x * y | x = 50 | [Example Β»](#) |
| /= | x /= y | x = x / y | x = 2 | [Example Β»](#) |
| %= | x %= y | x = x % y | x = 0 | [Example Β»](#) |
For more information on assignment operators, you can read our (#).
* * *
## JavaScript String Operators
The + operator, and the += operator, can be used to concatenate (join) strings.
Given **text1 = "Good "** , **text2 = "Morning"**, and **text3 = ""**, the following table explains the use of string operators:
| Operator | Example | text1 | text2 | text3 | Online Example |
| --- | --- | --- | --- | --- | --- |
| + | text3 = text1 + text2 | "Good " | "Morning" | "Good Morning" | [Example Β»](#) |
| += | text1 += text2 | "Good Morning" | "Morning" | "" | [Example Β»](#) |
* * *
## Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given **x=5**, the following table explains the comparison operators:
| Operator | Description | Comparison | Result | Online Example |
| --- | --- | --- | --- | --- |
| == | Equal to | x == 8 | false | [Example Β»](#) |
| x == 5 | true | [Example Β»](#) |
| === | Equal value and equal type (Strict equality) | x === "5" | false | [Example Β»](#) |
| x === 5 | true | [Example Β»](#) |
| != | Not equal | x != 8 | true | [Example Β»](#) |
| !== | Not equal value or not equal type (Strict inequality) | x !== "5" | true | [Example Β»](#) |
| x !== 5 | false | [Example Β»](#) |
| > | Greater than | x > 8 | false | [Example Β»](#) |
| < | Less than | x = | Greater than or equal to | x >= 8 | false | [Example Β»](#) |
| <= | Less than or equal to | x <= 8 | _true_ | [Example Β»](#) |
For more information on comparison operators, you can read our (#).
* * *
## Conditional Operator
The conditional operator is used to perform a condition-based assignment.
Given **x=6 and y=3**, the following table demonstrates the conditional operator:
| Syntax | Example | Online Example |
| --- | --- | --- |
| _variable_ = (_condition_) ? _value1_ : _value2_ | voteable = (age < 18) ? "Too young" : "Old enough"; | [Example Β»](#) |
* * *
## Logical Operators
Logical operators determine the logical relationship between variables or values.
Given **x=6 and y=3**, the following example demonstrates the use of logical operators:
| Operator | Description | Example |
| --- | --- | --- |
| && | And | (x 1) is true |
| || | Or | (x == 5 || y == 5) is false |
| ! | Not | !(x == y) is true |
* * *
## JavaScript Bitwise Operators
Bitwise operators work on 32-bit numbers. Any numeric operand is converted to a 32-bit integer. The result is converted back to a JavaScript number.
| Operator | Description | Example | Similar to | Result | Decimal |
| --- | --- | --- | --- | --- | --- |
| & | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
| ~ | NOT | x = ~ 5 | ~0101 | 1010 | -6 |
| ^ | XOR | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
| << | Left shift | x = 5 << 1 | 0101 <> | Right shift | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |
YouTip