Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript operators

JavaScript operators


JavaScript operators are used to assign values, compare values, perform arithmetic operations, and so on.


JavaScript arithmetic operators

Arithmetic operators are used to perform operations on two variables or values.

Assignment y = 5, the following table will show you how to use 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 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 arithmetic operators, you can read our JavaScript operators tutorial.


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 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 »

For assignment operators, you can read our JavaScript operators tutorial.


JavaScript string operators

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 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 example
== Equal x == 8 false Example »
x == 5 true Example »
=== Equal in both value and type (strictly equal) x === "5" false Example »
x === 5 true Example »
!= Not equal x != 8 true Example »
!== Unequal in both value and type (not strictly 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 »

For comparison operators, you can read our JavaScript comparison operators tutorial.


Conditional operator

Conditional operators are used for assignment based on a condition.

Given x=6 and y=3, the following table demonstrates the use of conditional operators:

Syntax Example Online example
Variables = (Conditional) ? 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.

Given x=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 operation is converted to 32 bits. 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
~ Bitwise 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