30 min·beginner

Lesson 5

Operators & comparisons

Build a practical mental model for operators & comparisons and use it in working JavaScript.

By the end, you will be able to…

  • Explain operators & comparisons in your own words
  • Use operators & comparisons in a small program
  • Recognize and debug a common failure mode

Build the mental model

== coerces both sides to a common type before comparing, which hides bugs; === compares type and value together, which is why professional style guides mandate it almost everywhere.

Syntax and working example

Read the code from inputs to output. Predict what it prints, then run it and change one value.

Working example
Expected output: true false 1

Why this matters

A single accidental == instead of === has shipped real production bugs; strict equality removes an entire class of 'why is this true' surprises.

Real-world example

ESLint's eqeqeq rule, enabled in nearly every company style guide, exists purely because of this lesson.

Common mistakes

  • Using == out of habit and getting unexpected coercion
  • Forgetting that NaN === NaN is false
  • Confusing = (assignment) with == or === (comparison)

Try it yourself

Challenge

Replace the loose comparisons below with strict ones and predict which results change.

Practice

Knowledge check

Why do most style guides forbid == in favor of ===?

Summary

Default to === and !==; reach for == only with a deliberate, documented reason.