JavaScript Operators
Introduction
Operators are special symbols in JavaScript that perform operations on values and variables.
For example:
let a = 10;
let b = 5;
console.log(a + b);
Output:
15
Here, + is an operator used to add two values.
JavaScript provides several types of operators that help us perform arithmetic calculations, comparisons, logical operations, assignments, and much more.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| - | Subtraction | 10 - 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| % | Modulus (Remainder) | 10 % 3 |
| ** | Exponentiation | 2 ** 3 |
Example
let a = 10;
let b = 5;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
console.log(a ** b);
Output:
15
5
50
2
0
100000
Special Behavior of +
The + operator performs both addition and string concatenation.
console.log("Bahubali" + 2);
Output:
Bahubali2
Type Coercion in Arithmetic Operations
console.log("15" - 5);
Output:
10
JavaScript automatically converts "15" into the number 15.
"15" → 15
15 - 5 = 10
2. Assignment Operators
Used to assign values to variables.
| Operator | Example | Equivalent |
|---|---|---|
| = | a = 10 | Assign value |
| += | a += 5 | a = a + 5 |
| -= | a -= 5 | a = a - 5 |
| *= | a *= 5 | a = a * 5 |
| /= | a /= 5 | a = a / 5 |
Example
let a = 10;
a += 5;
console.log(a);
Output:
15
3. Comparison Operators
Comparison operators compare two values and return either true or false.
| Operator | Description |
|---|---|
| == | Equal to |
| === | Strict equal to |
| != | Not equal |
| !== | Strict not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Double Equals (==)
Checks only values.
console.log(5 == "5");
Output:
true
Reason:
"5" → 5
5 == 5
Triple Equals (===)
Checks both value and datatype.
console.log(5 === "5");
Output:
false
Reason:
Number !== String
4. Logical Operators
Logical operators are commonly used in conditions.
| Operator | Meaning |
|---|---|
| && | AND |
| ! | NOT |
AND (&&)
Returns true only if all conditions are true.
let age = 25;
console.log(age > 18 && age < 60);
Output:
true
OR (||)
Returns true if at least one condition is true.
console.log(false || true);
Output:
true
NOT (!)
Reverses the boolean value.
console.log(!true);
Output:
false
5. Increment and Decrement Operators
Increment (++)
Adds 1.
let a = 5;
a++;
console.log(a);
Output:
6
Decrement (--)
Subtracts 1.
let a = 5;
a--;
console.log(a);
Output:
4
Pre-Increment vs Post-Increment
Post Increment
let a = 5;
console.log(a++);
console.log(a);
Output:
5
6
Pre Increment
let a = 5;
console.log(++a);
Output:
6
Interview Tricky Questions
Question 1
console.log("5" + 5);
Output:
55
Reason: String concatenation.
Question 2
console.log("5" - 5);
Output:
0
Reason: String converted to number.
Question 3
console.log(true + true);
Output:
2
Because:
true = 1
1 + 1 = 2
Question 4
console.log(false + 10);
Output:
10
Because:
false = 0
0 + 10 = 10
Question 5
console.log(10 + "5");
Output:
105
Question 6
console.log(10 - "5");
Output:
5
Question 7
console.log([] + []);
Output:
""
Empty string.
Question 8
console.log([] + {});
Output:
"[object Object]"
Question 9
console.log(null == undefined);
Output:
true
Question 10
console.log(null === undefined);
Output:
false
Because strict equality checks datatype as well.
Frequently Asked Interview Question
Difference Between == and ===
| == | === |
|---|---|
| Checks value only | Checks value and datatype |
| Performs type conversion | No type conversion |
| Less strict | More strict |
5 == "5" → true |
5 === "5" → false |
Interview Answer
== compares only values after type conversion, whereas === compares both values and datatypes without performing type conversion. Therefore, === is generally preferred in JavaScript applications.
References :
https://www.geeksforgeeks.org/javascript/javascript-operators/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_operators#arithmetic_operators



Top comments (0)