JavaScript operators
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 will explain to you 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» |
% | remainder | x = y % 2 | y = 5 | x = 1 | Instance» |
++ | increment | x = + +y | y = 6 | x = 6 | Instance» |
x = y++ | y = 6 | x = 5 | Example» | ||
-- | decrement | x = - -y | y = 4 | x = 4 | Instance» |
x = y-- | y = 4 | x = 5 | Example » |
For arithmetic operators, you can read our JavaScript operators tutorial .
JavaScript assignment operator
The assignment operator is used to assign values to JavaScript variables.
Given x=10 and y=5, the following table explains the assignment operators:
Operator | Example | Same As | 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» |
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 to judge logical statements to determine whether two given values or variables are equal.
Given x=5, the following table shows the use of comparison operators:
Operator | Description | Comparison | Result | Online instance | |
---|---|---|---|---|---|
== | equals | x == 8 | false | Instances» | |
x == 5 | true | Instances » | |||
=== | The value and type are equal (identical to) | x === "5" | false | Instance» | |
x === 5 | true | Instance» | |||
!= | is not equal to | x != 8 | true | Example» | |
The value and type are not equal (not equal) | x !== "5" | true | Instance» | ||
false | Instance» | ||||
Greater than | x > 8 | false | Example» | ||
Less than | x < 8 | true | Instance» | ||
is greater than or equal to | x >= 8 | false | Instance» | ||
is less than or equal to | x <= 8 | true | Example»
Syntax | Example | Online example |
---|---|---|
Variable= (Condition) ? Value 1:Value 2 | voteable = (age & 18) ? "Too young" : "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 example demonstrates 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 |
Bit operators work on 32-bit numbers. Any numeric operations will be converted to 32 bits. The result is converted to a JavaScript number.
Example | Similar to | Result | Decimal | & | |
---|---|---|---|---|---|
x = 5 & 1 | 0101 & 0001 | 0001 | 1 | ##| | |
x = 5 | 1 | 0101 | 0001 | 0101 | 5 | ##~ | Negation |
~0101 | 1010 | 10 | ##^ | XOR | |
0101 ^ 0001 | 0100 | 4 | ##<< | Shift left/td> | x = 5 << 1 |
1010 | 10 | >> | Shift right | x = 5 >> 1 | |
0010 | 2 |