JavaScript comparison operators are used to compare two values and return a boolean value (true or false) based on the comparison. These operators are essential in controlling the flow of a program, especially in conditional statements like if, while, and for loops.
1. Equality (==) Operator
Compares two values for equality after converting them to a common type (type coercion).
Returns true if the values are equal, otherwise false
10 == "10"; // true (number 10 is coerced to string "10")
20 == 20 // true (number 20 is coerced to number 20)
2. Strict Equality (===)
Compares two values for equality without performing type conversion. Both value and type must be the same.
10 === "10"; // false
10 === 10; // true
3. Inequality (!=)
Compares two values for inequality without performing type conversion.
10 !== "10"; // true
10 !== 10; // false
4. Strict Inequality (!==)
Compares two values for inequality without performing type conversion.
10 !== "10"; // true
10 !== 10; // false
5. Greater Than (>)
Returns true if the left operand is greater than the right operand.
10 > 5; // true
6. Greater Than or Equal To (>=)
Returns true if the left operand is greater than or equal to the right operand.
10 >= 10; // true
7. Less Than (<)
Returns true if the left operand is less than the right operand.
5 < 10; // true
8. Less Than or Equal To (<=)
Returns true if the left operand is less than or equal to the right operand.
5 <= 5; // true
9. Ternary Operator (? :)
A shorthand for an if-else statement. It takes three operands: a condition, a value if true, and a value if false.
const result = (10 > 5) ? "Yes" : "No"; // "Yes"
JavaScript comparison – Interview Questions
Q 1: What are comparison operators?
Ans: Operators used to compare values.
Q 2: What is the == operator?
Ans: Checks value equality (type conversion allowed).
Q 3: What is the === operator?
Ans: Checks value and type equality.
Q 4: What does != mean?
Ans: Not equal.
Q 5: What does > operator do?
Ans: Checks if left value is greater.
JavaScript comparison – Objective Questions (MCQs)
Q1. Which operator checks value only?
Q2. Which operator checks value and type?
Q3. What is the result of 5 == "5"?
Q4. What is the result of 5 === "5"?
Q5. Which operator means “not equal value or type”?