Operators in JavaScript are used to perform operations on variables and values. They can be classified into different types based on their functionality.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 2 |
7 |
- |
Subtraction | 5 - 2 |
3 |
* |
Multiplication | 5 * 2 |
10 |
/ |
Division | 10 / 2 |
5 |
% |
Modulus (Remainder) | 10 % 3 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
let a = 10;
let b = 5;
console.log(a + b); // Output: 15
Operator | Description | Example |
---|---|---|
= |
Assign value | x = 10 |
+= |
Add and assign | x += 5 (same as x = x + 5 ) |
-= |
Subtract and assign | x -= 5 |
*= |
Multiply and assign | x *= 5 |
/= |
Divide and assign | x /= 5 |
let x = 10;
x += 5; // x = x + 5;
console.log(x); // Output: 15
true
or false
)Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to (checks value) | 5 == "5" |
true |
=== |
Strict equal (checks type & value) | 5 === "5" |
false |
!= |
Not equal | 5 != 3 |
true |
!== |
Strict not equal | 5 !== "5" |
true |
> |
Greater than | 10 > 5 |
true |
< |
Less than | 10 < 5 |
false |
>= |
Greater than or equal | 10 >= 10 |
true |
<= |
Less than or equal | 5 <= 3 |
false |
console.log(10 > 5); // Output: true
console.log(5 === "5"); // Output: false
Operator | Description | Example | Result |
---|---|---|---|
&& |
AND (Both true) | true && false |
false |
` | ` | OR (At least one true) | |
! |
NOT (Reverse) | !true |
false |
let isAdult = true;
let hasID = false;
console.log(isAdult && hasID); // Output: false
Operator | Description | Example |
---|---|---|
& |
AND | 5 & 1 |
` | ` | OR |
^ |
XOR | 5 ^ 1 |
~ |
NOT | ~5 |
<< |
Left Shift | 5 << 1 |
>> |
Right Shift | 5 >> 1 |
console.log(5 & 1); // Output: 1 (Binary: 101 & 001 = 001)
Operator | Description | Example |
---|---|---|
+ |
Concatenation | "Hello " + "World" |
+= |
Append and assign | text += "JS" |
let str = "Hello ";
str += "World!";
console.log(str); // Output: Hello World!
? :
) (Shortens if-else
)Syntax:
condition ? value_if_true : value_if_false;
let age = 20;
let message = (age >= 18) ? "Adult" : "Minor";
console.log(message); // Output: Adult
if-else
conditions.
@asadmukhtar