Overview and Lesson Plan

Greetings, Explorer! Today, we're delving into JavaScript Comparison Operators. Decisions in life and programming often stem from comparisons. Our journey today will demonstrate how comparison operators in JavaScript compare values, forming rules that impact decision-making.

Introduction to JavaScript Comparison Operators

Life is about comparisons, just like JavaScript. JavaScript comparison operators such as ==, !=, ===, !==, >, <, >=, <= enable decision-making. Consider the following comparison of two numbers:

let number5 = 5;
let number10 = 10;

console.log(number5 == number10); // false, because 5 is not equal to 10
console.log(number5 === number10); // false, because 5 is not equal to 10
console.log(number5 != number10); // true, because 5 is indeed not equal to 10
console.log(number5 !== number10); // true, because 5 is indeed not equal to 10
console.log(number5 > number10); // false, because 5 is not greater than 10
console.log(number5 < number10); // true, because 5 is less than 10
console.log(number5 >= number10); // false, because 5 is neither greater than nor equal to 10
console.log(number5 <= number10); // true, because 5 is less than or equal to 10

In the example above, we covered all comparison operators, and we are going to discuss some of them in detail in the next sections.

Diving Deep into JavaScript Comparison Operators

As we dive deeper into JavaScript comparison operators, take note of the differences between double equals (==) and triple equals (===), and not equals (!=) and not strictly equals (!==) operators.

  • Equal to (==), Not equal to (!=) - These consider value only, disregarding type.
console.log(5 == "5"); // true, because the value 5 equals the string "5", even though the types are different
console.log(5 != "5"); // false, because they are equal in value
  • Strictly equal to (===), Not strictly equal to (!==)
    • These consider both value and type.
console.log(5 === "5"); // false, their types differ (number and string)
console.log(5 !== "5"); // true, as both type and value don't match

So, as you can see, === and !== are basically safer if you need to make sure that compared variables not only have the same value but also the same type. Understanding these differences is absolutely essential for decision-making in programming!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal