Skip to article content
FrontendXLesson 01 / 30

Values, Types and Equality

Build a precise mental model of JavaScript values, scope, functions, objects, arrays and asynchronous behavior, then implement and debug real utilities from first principles instead of memorizing outputs.

beginner · about 35 minutes · 6 exercises

  • Predict
  • Explain
  • Implement
  • Debug
  • Test
  • Interview

What you will be able to do

#
  • Classify any JavaScript value as a primitive or an object.
  • Choose between ==, === and Object.is and justify the choice.
  • List the falsy values and avoid truthiness bugs.

Overview

#

Why values come first

Every JavaScript program is a sequence of operations on values. Before you can reason about closures, promises or components, you need a precise model of what a value is, how it is stored, and how equality works. Most confusing bugs in beginner code trace back to one of three questions: is this a primitive or a reference, did I compare values or references, and is this value truthy?

The two families of values

JavaScript has two kinds of values. Primitives are immutable and copied by value: string, number, boolean, undefined, null, symbol and bigint. Objects, which include arrays, functions, dates and maps, are mutable and copied by reference. When you assign an object to another variable you copy the reference, not the object.

snippet.js
const a = { count: 1 };
const b = a;
b.count = 2;
console.log(a.count); // 2, both names point at one object

typeof and its historical quirks

The typeof operator returns a string describing a value, but two cases surprise people. typeof null returns "object" for legacy reasons, and typeof [] also returns "object" because arrays are objects. Functions report "function". Use Array.isArray to detect arrays and an explicit value === null check for null.

Equality: loose, strict and same-value

== performs type coercion before comparing, which produces rules that are hard to remember. === compares without coercion and is the default choice. Neither treats NaN as equal to itself. Object.is is the strictest comparison and is the only built-in that considers NaN equal to NaN and distinguishes +0 from -0.

snippet.js
console.log(0 == "0");          // true, the string is coerced to a number
console.log(0 === "0");         // false, different types
console.log(NaN === NaN);         // false
console.log(Object.is(NaN, NaN)); // true

Truthiness

Values coerce to boolean in conditions. The falsy set is small: false, 0, -0, 0n, the empty string, null, undefined and NaN. Everything else, including empty arrays and empty objects, is truthy. Relying on truthiness is fine when you control the values, but check for an empty array with items.length === 0, not with a bare if (items).

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What does this line print?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.javascript
    console.log(typeof null, typeof [], typeof (() => {}), typeof NaN);
    Choose the best answer
  2. Exercise 2Explain

    Explain why `0 == "0"` is true while `0 === "0"` is false.

    starter.javascript
    console.log(0 == "0");
    console.log(0 === "0");

    Reveal the reference answer
    reference.javascript
    The abstract equality algorithm used by == coerces the operands to a common type before comparing. When one side is a number and the other a string, the string is converted to a number, so "0" becomes 0 and the comparison is true. The strict equality algorithm used by === compares types first and returns false immediately when they differ, so no coercion happens.

    Authored expected output: true false

    Coercion is the whole difference. == runs the abstract equality comparison, which converts "0" to the number 0. === runs the strict equality comparison, which rejects operands of different types.

  3. Exercise 3Implement

    Implement `isPrimitive(value)` so that it returns true only for the seven primitive types. Running `console.log(isPrimitive(1), isPrimitive({}), isPrimitive(null), isPrimitive('x'), isPrimitive(() => {}))` should print `true false true true false`.

    starter.javascript
    function isPrimitive(value) {
      // your code here
    }

    Reveal the reference answer
    reference.javascript
    function isPrimitive(value) {
      return value === null || (typeof value !== "object" && typeof value !== "function");
    }

    Authored expected output: true false true true false

    null is a primitive even though typeof reports "object", so it is checked explicitly. Everything that is not an object or a function is a primitive.

  4. Exercise 4Debug

    This function is meant to detect an empty array but reports the wrong result for `[]`. Find and fix the bug.

    starter.javascript
    function isEmpty(items) {
      return !items;
    }
    console.log(isEmpty([]), isEmpty([1]));

    Reveal the reference answer
    reference.javascript
    function isEmpty(items) {
      return Array.isArray(items) && items.length === 0;
    }
    console.log(isEmpty([]), isEmpty([1]));

    Authored expected output: true false

    An empty array is truthy, so `!items` is false for `[]`. The fix checks the length instead of relying on truthiness, and Array.isArray guards against non-array input.

  5. Exercise 5Test

    Write assertions that prove Object.is differs from === for NaN and for signed zero.

    starter.javascript
    function assertEqual(actual, expected, label) {
      if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual);
    }

    Reveal the reference answer
    reference.javascript
    assertEqual(Object.is(NaN, NaN), true, 'NaN is same value');
    assertEqual(NaN === NaN, false, 'NaN is not strictly equal');
    assertEqual(Object.is(0, -0), false, 'signed zero differs');
    assertEqual(0 === -0, true, 'signed zero is strictly equal');
    console.log('all assertions passed');

    Authored expected output: all assertions passed

    Object.is treats NaN as equal to itself and separates +0 from -0, while === does the opposite in both cases. The assertions fail loudly if a future engine changes either behavior.

  6. Exercise 6Interview

    In an interview, explain the difference between primitive and reference values and give one bug each can cause.

    starter.javascript
    const a = { n: 1 };
    const b = a;
    b.n = 2;
    console.log(a.n);

    Reveal the reference answer
    reference.javascript
    Primitives are copied by value; objects are copied by reference. A reference bug: mutating `b` also changes `a` because both point at one object. A primitive bug: assuming that reassigning a parameter inside a function changes the caller's variable, when the function only received a copy of the primitive.

    Authored expected output: 2

    The example prints 2 because `a` and `b` share one object. A strong answer names the storage model, not just the symptom.

Common misconceptions

#
  • typeof null returning "object" is a cosmetic quirk that does not affect type checks.
  • == is just a shorter way to write === and always gives the same result.
  • Empty arrays and empty objects are falsy in conditions.

Follow-up practice

#
  • javascript-foundations-scope-and-hoisting
  • javascript-foundations-functions-and-closures

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.