Comparing Values with === and Other Operators
Introduction
Welcome back! We have learned that values can have different types and that we can convert between text and numbers when a calculation needs it. Now we can compare values to ask questions such as whether stock is below a reorder level.
In this lesson, we will use comparison operators to produce booleans, store those answers, and check equality with === and !==. We will also see why the loose equality operator == can give a surprising result.
A Comparison Produces a Boolean
A comparison checks a relationship between two values and produces a boolean: true when the relationship holds, or false when it does not. For example, we can check whether a stock count is below a reorder level:
The first question is true because 4 is below 10. The second question is false because 4 is not greater than or equal to 10. When the program runs, we see:
The symbols < and >= compare numbers here. We can use > for greater than, < for less than, >= for greater than or equal to, and <= for less than or equal to. For example, stockCount <= reorderLevel is true when the stock count is 10 or less.
Store a Comparison Result
A comparison's boolean result is a value just like a number or string. We can store it in a variable and use it later:
JavaScript checks the comparison on the right side of =, stores the result under needsReorder, and then prints its value and type:
The name needsReorder describes the question we asked. Clear names become especially helpful when a boolean will be used in several places.
Check Whether Values Match
For the simple values in this lesson, we use three equals signs, ===, to check that both the value and type match without converting one side. To check that they differ, we use !==:
The first two comparisons are true. The last is false because the number 4 and the string "4" have different types:
This strict comparison is useful because it does not silently convert one type to match the other. In this path, === is the equality operator we use in our own code, and !== is its opposite.
Why We Avoid ==
JavaScript also has ==, which compares values loosely and may convert one side before checking. That can make two different types appear equal:
The loose comparison says true, while the strict comparison says false:
The first answer can hide the fact that one value is a number and the other is text. Since === gives us a clear answer without that automatic conversion, we use it consistently.
Choosing the Comparison That Matches the Question
The wording of a question tells us which comparison to use. > and < exclude an equal value; >= and <= include it. === asks whether these simple values match without type conversion, and !== asks whether they differ.
We can test an equal boundary directly:
The first question asks whether stock is strictly below the level. The second includes equality:
That distinction matters for “reorder below 10” versus “reorder at 10 or below.” We can say each question aloud before choosing its symbol. Every comparison produces a boolean that we can print, store, or insert into a template literal. Comparing values does not change them, so we can use them again in another question.
Write a Stock Report
Let's use several comparisons in a stock report with a different count. Template literals put each boolean answer into a labelled line:
The output answers three distinct questions:
The count is above the reorder level and above zero. No special reporting feature is needed; each line evaluates a comparison and places its boolean result into a sentence. The practice will ask us to make a different report from the same kinds of values.
Conclusion and Next Steps
We learned that >, <, >=, <=, ===, and !== produce boolean answers. We can store a comparison result under a clear name, and we use === to check equality without automatic type conversion. The example with 4 and "4" showed why the type matters.
Next, we will combine boolean answers with &&, ||, and !. That will let us express a decision that depends on more than one check.
