Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 15

Comparing Values with Operators

This lesson is built with resources from youtube.com, developer.mozilla.org and 1 more site.

Hello! Welcome to the next step in our JavaScript journey.

In the last lesson, we saw how operators can perform calculations (like * and /) and combine strings (using + or template literals). We also got our first glimpse of an interesting JavaScript behavior called type coercion, where '34' + 1 unexpectedly became the string '341'.

Today, we will explore a new family of operators: comparison operators. These are the tools that allow our programs to ask questions like "is this value bigger than that one?" or "are these two things equal?". The answer to these questions is always a simple boolean value: either true or false. Mastering these operators is the key to creating logic and making decisions in your code, which is a fundamental concept we'll need for everything ahead, especially building dynamic Next.js applications.

Our goal is to learn how to use all the main comparison operators, with a special focus on the most important and often misunderstood concept for beginners: the difference between loose equality (==) and strict equality (===).

Part 1: Relational Operators - Comparing Sizes

First, let's look at the operators that compare the size or magnitude of two values. You'll likely recognize these from math class.

  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

These operators take two values and return true or false based on the comparison. For example, the expression 10 > 5 evaluates to true.

Let's watch a video that provides a clear, visual explanation of these operators in action.

JavaScript Comparison Operators Explained

The video 'JavaScript Comparison Operators Explained' from the Future Fullstack channel has excellent animations to demonstrate how relational operators work.

Watch the section from 00:49 to 04:15. Pay attention to how each operator handles cases where the numbers are equal (e.g., 5 > 5 is false, but 5 >= 5 is true).

As you saw, the concept is straightforward for numbers. It's also worth knowing that these operators can compare strings, where they check for alphabetical order. For example, 'apple' < 'banana' would be true.


Part 2: Equality - The Tale of Two Equals

Now we get to the most critical part of this lesson: comparing for equality. In JavaScript, there are two ways to do this, and understanding the difference is crucial for writing robust, bug-free code.

  1. Loose Equality (==): Checks if the values are equal, but it might perform type coercion first. It's "loose" because it doesn't care if you're comparing a number to a string.
  2. Strict Equality (===): Checks if the values are equal and if they are of the same data type. It is "strict" and does not perform type coercion.

This visual illustrates the core difference perfectly.

Loose vs. Strict Equality in JavaScript
This illustration shows that with loose equality (`==`), JavaScript might see a string "5" and a number 5 as the same thing. With strict equality (`===`), they are correctly identified as different because their types don't match.

Let's dive deeper into how each one works.


Part 3: Loose vs. Strict in Practice

The best way to understand the difference is to see it in action and hear the reasoning behind it. The same video we watched earlier explains this beautifully.

JavaScript Comparison Operators Explained

Let's return to the 'JavaScript Comparison Operators Explained' video. The instructor will walk through loose and strict equality, and then their 'not equal' counterparts, != and !==.

Please watch from 04:57 to 12:56. The video is broken down as follows: Loose Equality (==): Pay attention to the explanation of type coercion (04:57). Strict Equality (===): Notice why this is more predictable (06:05). Loose Inequality (!=) & Strict Inequality (!==): These follow the same rules (07:07). VS Code Demo: Seeing it all in the console solidifies the concepts (10:48).

As the video emphasized, the unpredictable nature of type coercion with == and != can introduce hidden bugs into your code. For this reason, a universal best practice in modern JavaScript is to always use strict equality (===) and strict inequality (!==).


Part 4: The Deep Dive - Why They Behave Differently

Since you're interested in the "why" behind the code, let's look at the official documentation. The MDN (Mozilla Developer Network) web docs are the gold standard for web developers. The language can be technical, but it provides the most precise explanation of how JavaScript works under the hood.

Equality comparisons and sameness - JavaScript | MDN

The MDN article 'Equality comparisons and sameness' provides the definitive explanation. We'll focus on the sections that explain loose and strict equality and the summary table.

Please review the following parts of the article: Read the section "Strict equality using ===". It explains the clear rules this operator follows. Skim the section "Loose equality using ==". Don't worry about memorizing all the steps; the goal is to see how many complex rules and conversions are involved, reinforcing why we avoid it. Carefully study the table under the heading "Comparing equality methods". This table is a powerful visual summary and an excellent reference for seeing how different comparisons (like 0 == false vs 0 === false) evaluate.

The key takeaway from the MDN article is the sheer complexity of loose equality (==) compared to the simple, direct logic of strict equality (===). By choosing ===, you are choosing clarity and predictability.

Test your understanding!

For each expression below, determine what it will evaluate to (true or false) and, most importantly, why.

  1. 25 > 100
  2. '10' === 10
  3. '10' == 10
  4. 0 == false
  5. null === undefined
  6. null == undefined
  7. 12 <= 12
  8. 'A' > 'a'
Show answer
  1. false. 25 is not greater than 100.
  2. false. This is strict equality. While the values are conceptually the same, the types are different (string and number).
  3. true. This is loose equality. JavaScript performs type coercion, converting the string '10' to the number 10 before making the comparison.
  4. true. This is another classic loose equality example. JavaScript coerces false to the number 0 before comparing. 0 === false would be false.
  5. false. With strict equality, null and undefined are not the same because they are of different types.
  6. true. This is the one major exception where loose equality is sometimes seen. == treats null and undefined as equal to each other, and only to each other.
  7. true. 12 is not less than 12, but it is equal to 12. The <= operator checks for either condition.
  8. false. When comparing strings, JavaScript compares their underlying character codes. Uppercase letters have lower values than lowercase letters, so 'A' is considered "less than" 'a'.

Conclusion

Excellent work! You've just navigated one of the most common tripping points for new JavaScript developers. Understanding comparison is fundamental to building any sort of logic.

Here is a handy "cheat sheet" to summarize what we've learned today.

Comparison Operators Cheat Sheet
This table serves as a great quick reference for all the operators covered in this lesson.

Key Takeaways:

  • Comparison operators ask questions about the relationship between two values and always result in a boolean (true or false).
  • Relational operators (>, <, >=, <=) compare the magnitude of values.
  • Loose equality (==) checks only for value, which can lead to bugs due to implicit type coercion.
  • Strict equality (===) checks for both value and type, making it predictable and safe.
  • The Golden Rule: Always prefer strict comparison (=== and !==) to avoid unexpected behavior.

Next Up:

So, we can now ask questions and get true or false back. But what do we do with that answer? In our next lesson, we will begin exploring Control Flow. We'll learn to use if statements to tell our program: "IF this condition is true, then run this block of code." This is where your code truly starts to come alive and make decisions.