Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

JavaScript Values, Types, Coercion, and Equality

Build a reliable mental model for JavaScript language types, primitive values, objects, truthiness, explicit conversion, coercion, equality, and comparison edge cases.

Levels: Intermediate ~30 min read

Intermediate

JavaScript becomes much easier to predict when you stop treating every value as “just data.” Every expression produces a value, every value has an ECMAScript language type, and many operators can convert values as part of evaluation. This lesson builds that model so equality and coercion stop feeling like a collection of surprises.

1. JavaScript has a small set of language types

The ECMAScript language types are Undefined, Null, Boolean, String, Symbol, Number, BigInt, and Object. A value is not the same thing as a variable name: a binding can point to a value, and the value has its own type.

Interactive Runnable example
Edit

Ready to run.

Quick check

Which item is an ECMAScript language type?

const result = ;

2. Primitives and objects behave differently

Undefined, null, booleans, strings, symbols, numbers, and BigInts are primitive values. Objects have identity and can carry properties. This distinction matters when you compare values or mutate data: two separately created objects are distinct objects even if their properties look identical.

Interactive Runnable example
Edit

Ready to run.

Quick check

Why is { score: 10 } === { score: 10 } false?

const result = ;

3. Truthiness is a conversion to Boolean

When JavaScript needs a Boolean for a condition, it applies the language’s Boolean conversion. Values such as false, 0, NaN, "", null, and undefined are falsy. Objects and non-empty strings are truthy. Truthiness is a conversion rule; it does not change the original value.

Interactive Runnable example
Edit

Ready to run.

Quick check

Which value is falsy?

const result = ;

4. Explicit conversion makes intent visible

Use Boolean when you explicitly want a Boolean, Number for numeric conversion, and String for string conversion. Conversion is not the same as checking a type: typeof reports a type, while these functions ask for a converted value.

Interactive Runnable example
Edit

Ready to run.

Quick check

What does Number("42") produce?

const result = ;

5. Numeric conversion can produce NaN

Not every string represents a valid Number. When numeric conversion cannot produce a numeric result, it can produce NaN, the special Number value meaning “not a number.” NaN is still a Number value, so typeof NaN is "number". Use Number.isNaN when you specifically need to test for NaN.

Interactive Runnable example
Edit

Ready to run.

Quick check

Which test specifically checks whether a value is NaN?

const result = ;

6. Strict equality compares without cross-type coercion

The === and !== operators use strict equality. Values of different types are not strictly equal, so 42 and "42" are different values for this comparison. For Numbers, NaN is not equal to NaN, while +0 and -0 compare as equal under strict equality.

Interactive Runnable example
Edit

Ready to run.

Quick check

What is 42 === "42"?

const result = ;

7. Loose equality has a coercion algorithm

The == and != operators perform abstract equality comparison. When operand types differ, the specification defines conversion steps before the final comparison. For example, 42 == "42" is true. That behavior is part of the language; it is not string comparison secretly becoming numeric comparison by accident.

Interactive Runnable example
Edit

Ready to run.

Quick check

Why can 42 == "42" be true while 42 === "42" is false?

const result = ;

8. Relational comparisons also depend on operand types

The <, >, <= and >= operators can compare strings lexicographically or compare numeric values depending on the operands and conversion rules. A common surprise is that "10" < "2" is true because both operands are strings and their code-unit sequences are compared.

Interactive Runnable example
Edit

Ready to run.

Quick check

What does "10" < "2" evaluate to?

const result = ;

9. BigInt does not silently mix with Number arithmetic

BigInt represents arbitrary-precision integers. It is deliberately distinct from Number for arithmetic. Do not assume JavaScript will silently convert a Number and a BigInt during arithmetic: 1n + 1 throws a TypeError. Explicit conversion is required, and converting a Number to BigInt is only valid when the Number can represent an integer exactly enough for that conversion.

Interactive Runnable example
Edit

Ready to run.

Quick check

What happens when JavaScript evaluates 1n + 1?

const result = ;

10. Same-value concepts explain a few equality edge cases

Strict equality is not the only equality concept in ECMAScript. Object.is uses the language’s SameValue semantics: unlike ===, it treats +0 and -0 as different, and it treats NaN as equal to itself. Collection methods such as includes use a related SameValueZero comparison that also considers NaN equal to itself but treats +0 and -0 as the same.

Interactive Runnable example
Edit

Ready to run.

Quick check

Which comparison treats NaN as equal to itself?

const result = ;

11. Use the conversion or equality rule that matches your intent

  • Prefer explicit conversion such as Number(value), String(value), or Boolean(value) when the conversion is part of your intent.

  • Use === and !== for ordinary equality checks when you want to avoid implicit cross-type coercion.

  • Learn the == algorithm rather than relying on folklore; if you use it, make the intended coercion obvious to the reader.

  • Remember that truthiness is a Boolean conversion rule, not a test that a value is literally true or false.

  • Do not mix Number and BigInt in arithmetic without an explicit, justified conversion.

  • Use Number.isNaN(value) when you specifically need to recognize NaN, and Object.is when SameValue behavior matters.

Practice what you learned

Use the values, types, coercion, and equality code-completion exercise and then take the values, types, coercion, and equality quiz.

Specification reference

This lesson is based on the current ECMAScript Language Specification (ECMA-262), especially ECMAScript Data Types and Values, abstract conversion operations, equality comparison, and relational comparison. Read the specification when you need the exact algorithmic rule.

ECMA-262 — Data Types and Values

ECMA-262 — Type Conversion and Abstract Operations

ECMA-262 — Equality Operators

ECMA-262 — Relational Operators

Next step

Next, deepen the execution model with blocks, declarations, switch, break and continue, return, throw, try/catch/finally, and the practical rules around automatic semicolon insertion.

Was this page helpful?

Mark this learning resource complete to track your learning progress.