Comparison operators ask a question and answer with a boolean: > greater than, < less than, >= at least, <= at most, === equal, !== not equal.
=== (strict equality) is true only when both the value and the type match: 5 === 5 is true, "5" === 5 is false. Use === and !== by default.
== (loose equality) converts types before comparing, so "5" == 5 is true and 0 == "" is true too. These surprises cause bugs, which is why professionals avoid ==.
Strings are compared character by character: "apple" < "banana" is true. Capital letters come before lowercase ones ("Zebra" < "apple" is true), so convert case first when comparing user input.
Strict Equality Comparison never coerces; Abstract (loose) Equality applies a set of coercion rules (null == undefined, number vs string → ToNumber, boolean → ToNumber). The common exception in style guides is x == null to test for null or undefined.
Relational operators coerce to primitives and then to numbers unless both operands are strings, which are compared by UTF-16 code units. For human-language ordering use a.localeCompare(b) or Intl.Collator.
NaN is not equal to anything, including itself; Object.is(NaN, NaN) is true and Object.is(0, -0) is false, which matters for some algorithms.
Objects and arrays compare by reference: [1] === [1] is false because they are two different objects. Comparing contents requires a deep comparison.
05Definitions
Comparison operator
An operator that compares two values and gives true or false.
Strict equality (===)
True when two values have the same type and value.
Loose equality (==)
Converts types before comparing; can give surprising results.
!==
Strict not-equal.
Lexicographic order
Comparing text character by character, like a dictionary but by character code.
A checkout compares the promo code a shopper typed with the valid code. Shoppers type with spaces and in any case, so the comparison must normalise both values and use ===.
6. Which is the professional default for equality?
==
===
Both are equivalent
Neither
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Check the verification code: Prints Correct: true; Still uses ===
Case-insensitive match: Prints Same email: true; Normalises both sides
Price rule checks: Prints three true values; The booleans are correct; Uses comparisons, not literals
ex-011-01: output matches the documented expected output
ex-011-02: output matches the documented expected output
ex-011-03: output matches the documented expected output
ex-011-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-03-decisions-and-logic/lesson-011-comparison-operators/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Comparisons give booleans.
Use === and !==; == converts types and surprises.
Strings compare by character codes: normalise case and spaces.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanation=== versus ==
Context: I am learning equality in JavaScript.
Task: Explain === and == with six examples, three of them surprising.
Constraints: Beginner level.
Expected behaviour: I can explain why === is preferred.
Testing: Give me four comparisons to predict.
Code predictionComparison drill
Context: I know <, >, <=, >=, === and !==.
Task: Ask me ten comparison questions with numbers and strings.
Constraints: Wait for answers.
Expected behaviour: Each has one answer.
Testing: Explain string ordering for any I miss.
DebuggingComparison always false
Context: My check of a typed code against a saved code is always false.
Task: Help me find out why with questions about types and spaces.
Constraints: No direct answer at first.
Expected behaviour: I fix it myself.
Testing: Suggest logs that reveal the problem.
Exercise generationBusiness rules
Context: I finished the price rules exercise.
Task: Create two exercises that turn business rules into comparisons.
Constraints: Fictional data, exact output.
Expected behaviour: Each has three or more rules.
Testing: Include expected output.
Code reviewEquality review
Context: My script uses several == comparisons.
Task: Review which ones could misbehave and suggest replacements.
Constraints: Explain each risk briefly.
Expected behaviour: No == remains unless justified.
Testing: List the lines to change.