JavaScript Tutorial

JavaScript Comparisons

=== checks value and type. == coerces. Prefer === so "5" and 5 stay different.

Equal in two different ways

=== is strict equality: same value and same type. == is loose equality: JavaScript converts one side, then compares. That conversion is called coercion, and it is how"5" == 5 becomes true.

Prefer === in this tutorial and in your own scripts. When a string and a number should stay different, strict equality is the tool that keeps them different.

Log both operators in /javascript/try. The two results for"5" and 5 are the whole lesson.

=== checks value and type

Numbers compare as numbers. Strings compare as strings. A number is never strictly equal to a string of digits. true is not 1 under ===.

Example

console.log(5 === 5);
console.log("5" === "5");
console.log("5" === 5);
console.log(true === 1);
console.log(null === undefined);

document.body.textContent =
  String("5" === 5) + " strict / " + String(5 === 5) + " number";

== coerces

Loose equality converts before it compares. "5" == 5 is true because the string becomes the number 5. true == 1 is true. null == undefined istrue, which is a special case in the spec.

Example

console.log("5" == 5);
console.log(true == 1);
console.log(false == 0);
console.log("" == 0);
console.log(null == undefined);
console.log(null == 0);

document.body.textContent =
  String("5" == 5) + " loose / " + String("" == 0);

"" == 0 is true. An empty input from a form is not the number zero, but== will treat it that way. That is a real bug in score and quantity code.

Not equal

!== is the strict opposite of ===. != is the loose opposite of==. Use !== with the same discipline you use for ===.

OperatorTrue when
===Same value and same type
!==Different value or different type
==Equal after coercion
!=Not equal after coercion

Ordering

<, <=, >, and >= compare numbers in the obvious way. They also coerce: "10" > 2 is true because the string becomes a number. If both sides are strings, they compare alphabetically: "10" > "2" isfalse because "1" comes before "2".

Example

console.log(10 > 2);
console.log("10" > 2);
console.log("10" > "2");
console.log(7 <= 7);
console.log("apple" < "banana");

document.body.textContent =
  String("10" > 2) + " coerced / " + String("10" > "2") + " text";

NaN is never equal

NaN === NaN is false. NaN == NaN is also false. After a failed Number(...) conversion, use Number.isNaN(value), not an equality check.

Example

const n = Number("wait");
console.log(n === n);
console.log(n == n);
console.log(Number.isNaN(n));

document.body.textContent = "isNaN " + String(Number.isNaN(n));

What to remember

  • Default to === and !==.
  • "5" and 5 are different under strict equality.
  • Ordering coerces too. Convert on purpose, then compare numbers to numbers.
  • Never test NaN with ===.

Next: if, else if, and else — run a block when a test is true.

FAQ: JavaScript Comparisons

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript comparison operators examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript comparison operators in this JavaScript JavaScript lesson (JavaScript Comparisons).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.