Comparison Operators


Comparison Operators

🙋 Need help? Ask an expert now!

Comparison operators are used to compare two variables. These variables can be numbers, strings, booleans depending on the operator used. The comparison operators return true or false based on the operands used. Comparison operators are mostly used expressions to determine equality or difference between two operands. Below is a list of all the comparison operators:

==, <, >, >=, <=, !=,===,!==

For equality comparison, the use of === and !== is preferred. === checks if the two operands are of equal value and of equal type, while == just checks if they are of equal value.

A few comparison operators are shown above with comments on how they function. The toughest concept is the difference between == and ===. As stated in the paragraph above, the === operator checks if two operands have the same value AND are of the same data type, while the == operator checks if two operands hold the same value. This is why c == d prints true even though c is a number while d is an object. Therefore, c === d will print false even though they both hold the value 2.

Edit Me on GitHub!

Application Question

Consider the following code segment:

let a = 5;
let b = "5";
console.log(a == b && a === b);

What will the code above print to the console?