AI Hints
Home›JavaScript Tutorial›Comparing Values
JavaScript · Lesson 13 of 138

Comparisons

Equal, bigger, smaller.

⏱ 2 min read▶ Video at 14:18🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Comparisons ask questions about values, and answer with true or false. Is this bigger? Are these equal? They're the building blocks of every decision a program makes.

Comparisons
From the video: Comparisons
  • Questions about values
  • true or false
  • Building blocks of decisions

Comparison operators

Less than, and greater than. Less than or equal, and greater than or equal. Three equals signs check that two values are exactly equal. And exclamation mark, equals, equals checks that they're different. Strings can be compared too. Run it.

app.jsjavascript
console.log(5 < 10, 5 > 10);
console.log(10 <= 10, 9 >= 10);
console.log(7 === 7, 7 === 8);
console.log(7 !== 8);
console.log("apple" === "apple", "apple" < "banana");
Output
true false
true false
true false
true
true true

One, two, or three equals

One equals sign stores a value in a variable. It's not a question. Three equals signs ask, are these exactly the same? Mixing them up is one of the most common beginner bugs. And yes, there's also a two equals version. Next lesson, we'll see why you should avoid it.

keyvalue
=store a value
===exactly equal?
!==different?
== avoid, see next

Summary

Comparisons give true or false. Use three equals to check equality, and one equals to assign. Next, why three equals is better than two.

Key takeaways
  • < > <= >=
  • === and !==
  • = assigns
Next up: Type Coercion — why === beats ==