console.log("a" > 1);
console.log("02" > 1);
console.log("02" > "1");
Console Output:
false
true
false
console.log(null > 0);
console.log(null == 0);
console.log(null >= 0);
Console Output:
false
false
true
console.log(undefined == 0);
console.log(undefined > 0);
console.log(undefined < 0);
Console Output:
false
false
false
console.log("2" == 2);
console.log("2" === 2);
Console Output:
true
false
Difference Between == and === in JavaScript
In JavaScript, == and === are comparison operators, but they work differently:
==(Equality Operator) compares two values for equality after converting both values to a common type. This is known as type coercion.===(Strict Equality Operator) compares two values for equality without performing any type conversion. This means both the value and the type must be the same for the comparison to returntrue.
Consider the following examples:
console.log("2" == 2); // Output: true
console.log("2" === 2); // Output: false
Explanation:
- In the first comparison,
"2" == 2, JavaScript converts the string"2"to a number2before comparing, so the result istrue. - In the second comparison,
"2" === 2, JavaScript compares the string"2"with the number2without type conversion. Since the types are different (string vs. number), the result isfalse.