JavaScript All Operators
In JavaScript, operators are symbols that are used to perform specific operations on data or variables. They can be divided into several main categories:
1. Arithmetic Operators
Perform mathematical calculations on numbers.
+ (Addition) – Adds two numbers.
5 + 3 // 8
- (Subtraction) – Subtracts one number from another.
10 - 4 // 6
* (Multiplication) – Multiplies two numbers.
4 * 3 // 12
/ (Division) – Divides one number by another.
10 / 2 // 5
% (Modulo) – Returns the remainder of a division.
10 % 3 // 1
** (Exponentiation) – Raises a number to the power of another number.
2 ** 3 // 8
++ (Increment) – Increases a value by 1.
let x = 5
x++ // 6
-- (Decrement) – Decreases a value by 1.
let x = 5
x-- // 4
2. Assignment Operators
Assign values to variables.
= (Assignment) – Assigns a value to a variable.
let x = 10
+= (Addition assignment) – Adds a value and assigns the result.
let x = 5
x += 3 // 8
-= (Subtraction assignment) – Subtracts a value and assigns the result.
let x = 10
x -= 4 // 6
*= (Multiplication assignment) – Multiplies a value and assigns the result.
let x = 5
x *= 2 // 10
/= (Division assignment) – Divides a value and assigns the result.
let x = 10
x /= 2 // 5
%= (Remainder assignment) – Calculates remainder and assigns it.
let x = 10
x %= 3 // 1
**= (Exponent assignment) – Performs exponentiation and assigns the result.
let x = 2
x **= 3 // 8
3. Comparison Operators
Compare two values and return true or false.
== (Equality) – Checks if values are equal after type conversion.
5 == "5" // true
=== (Strict equality) – Checks if values and types are equal.
5 === "5" // false
!= (Inequality) – Checks if values are not equal.
5 != 3 // true
!== (Strict inequality) – Checks if values or types are not equal.
5 !== "5" // true
> (Greater than) – Checks if the left value is greater than the right.
10 > 5 // true
< (Less than) – Checks if the left value is less than the right.
5 < 10 // true
>= (Greater than or equal) – Checks if the left value is greater or equal.
10 >= 10 // true
<= (Less than or equal) – Checks if the left value is less or equal.
5 <= 10 // true
4. Logical Operators
Used to combine or invert boolean expressions.
&& (Logical AND) – Returns true if both conditions are true.
true && false // false
|| (Logical OR) – Returns true if at least one condition is true.
true || false // true
! (Logical NOT) – Reverses a boolean value.
!true // false
5. Bitwise Operators
Perform operations on binary representations of numbers.
& (AND) – Performs bitwise AND operation.
5 & 1 // 1
| (OR) – Performs bitwise OR operation.
5 | 1 // 5
^ (XOR) – Performs bitwise XOR operation.
5 ^ 1 // 4
~ (NOT) – Inverts all bits of a number.
~5 // -6
<< (Left Shift) – Shifts bits to the left.
5 << 1 // 10
>> (Right Shift) – Shifts bits to the right.
5 >> 1 // 2
>>> (Zero-fill Right Shift) – Right shift with zero fill.
5 >>> 1 // 2
6. String Operator
+ (Concatenation) – Combines two strings into one.
"Hello " + "World"
// "Hello World"
7. Conditional (Ternary) Operator
? : – A shorthand for an if-else condition.
let age = 18
let result = age >= 18 ? "Adult" : "Minor"
8. Type Operators
typeof – Returns the data type of a variable.
typeof "Hello"
// "string"
instanceof – Checks if an object is an instance of a constructor.
[] instanceof Array
// true
9. Optional Chaining Operator
?. – Safely accesses nested object properties without causing errors.
user?.profile?.name
10. Nullish Coalescing Operator
?? – Returns the right value if the left value is null or undefined.
let name = null
name ?? "Guest"
// "Guest"
11. Spread Operator
... – Expands elements of an array or object.
let arr = [1,2,3]
let newArr = [...arr,4]
12. Rest Operator
... – Collects multiple arguments into an array.
function sum(...numbers){
return numbers
}
13. in Operator
Checks if a property exists in an object.
"name" in {name:"Tanvir", age:25}
// true
14. delete Operator
Removes a property from an object.
let user = {name:"Tanvir", age:22}
delete user.age
15. Comma Operator
Executes multiple expressions and returns the last value.
let x = (1,2,3)
x // 3
