About Lesson
Comparison Operators
Used to compare two values:
- == (Equal): 5 == ‘5’ (true due to type coercion)
- === (Strict equal): 5 === ‘5’ (false, no type coercion)
- != (Not equal)
- !== (Strict not equal)
- <, >, <=, >= (Less than, greater than, etc.)
Example:
JavaScript
let a = 5;
let b = '5';
let c = 10;
// == (Equal with type coercion)
console.log(a == b); // true (5 is equal to '5' because of type coercion)
// === (Strict equal, no type coercion)
console.log(a === b); // false (5 is not strictly equal to '5' because their types are different)
// != (Not equal with type coercion)
console.log(a != b); // false (5 is considered equal to '5' with type coercion)
// !== (Strict not equal, no type coercion)
console.log(a !== b); // true (5 is not strictly equal to '5' because of type difference)
// < (Less than)
console.log(a < c); // true (5 is less than 10)
// > (Greater than)
console.log(c > a); // true (10 is greater than 5)
// <= (Less than or equal)
console.log(a <= b); // true (5 is equal to '5' with type coercion)
// >= (Greater than or equal)
console.log(c >= a); // true (10 is greater than 5)