JavaScript Operators
JavaScript operators are used to assign values, compare values, perform arithmetic operations, etc.
JavaScript Arithmetic Operators
Arithmetic operators are used to perform operations on two variables or values.
Assignmenty = 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 | 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 » |
About arithmetic operators, you can read ourJavaScript Operators Tutorial。
JavaScript Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
Givenx=10 and y=5, the following table explains the assignment operators:
| Operator | Example | Example | 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 » |
About assignment operators, you can read ourJavaScript Operators Tutorial。
JavaScript String Operators
The + operator and the += operator can be used to concatenate strings.
Giventext1 = "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 whether two given values or variables are equal.
Givenx=5, the following table shows the use of comparison operators:
| Operator | Description | Comparison | Result | Online Example |
|---|---|---|---|---|
| == | Equal to | x == 8 | false | Example » |
| x == 5 | true | Example » | ||
| === | Equal value and equal type (strictly equal) | x === "5" | false | Example » |
| x === 5 | true | Example » | ||
| != | Not equal | x != 8 | true | Example » |
| !== | Not equal value or type (strictly not equal) | x !== "5" | true | Example » |
| x !== 5 | false | Example » | ||
| > | Greater than | x > 8 | false | Example » |
| < | Less than | x < 8 | true | Example » |
| >= | Greater than or equal to | x >= 8 | false | Example » |
| <= | Less than or equal to | x <= 8 | true | Example » |
About comparison operators, you can read ourJavaScript Comparison Operators Tutorial。
Conditional Operator
The conditional operator is used to assign a value based on a condition.
Givenx=6 and y=3, the following table demonstrates the operation of the conditional operator:
| Syntax | Example | Online Example |
|---|---|---|
| Variable= (Condition) ?Value 1:Value 2 | voteable = (age < 18) ? "Too young to vote":"Old enough"; | Example » |
Logical Operators
Logical operators are used to determine the logical relationship between variables or values.
Givenx=6 and y=3, the following examples demonstrate the use of logical operators:
| Operator | Description | Example |
|---|---|---|
| && | and | (x < 10 && y > 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 32 bits. The result is converted 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 << 1 | 1010 | 10 |
| >> | Right shift | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |