Back to JavaScript Notes
Topic #349

JavaScript Data Types

By the end of this lesson, you can identify JavaScript’s primitive and reference data types, explain how they are stored and compared, and use typeof to inspect values safely.

What it is

JavaScript values fall into two broad groups: primitive types and reference types. A primitive value is a simple, immutable value. When you assign it to another variable, JavaScript copies the value. A reference value is an object-like value, such as an object, array, or function. When you assign it, JavaScript copies the reference, not the underlying data.

The primitive types are string, number, bigint, boolean, undefined, and symbol. null is a primitive value, but typeof null returns "object" because of an early implementation quirk. Related terms include immutability, reference, identity, and type coercion.

Why it matters

  • It explains why changing one object can affect another variable that points to the same object.
  • It helps you choose the right equality check for values and objects.
  • It prevents bugs when copying arrays or objects.
  • It makes debugging easier by clarifying what typeof can and cannot tell you.
  • It supports safer function design, especially when passing objects as arguments.

Syntax or steps

The smallest useful pattern is to inspect a value with typeof, then handle objects separately.

  1. Declare or receive a value.
  2. Use typeof value to get a type string.
  3. If the result is "object", check whether the value is null, an array, or a plain object.
  4. Compare primitives with ===; compare references by identity.

Example

const primitive = true;
const number = 42;
const text = "hello";
const missing = undefined;
const empty = null;

const object = { name: "Ada" };
const array = [1, 2, 3];
const functionValue = function () {};

console.log(typeof primitive); // "boolean"
console.log(typeof number); // "number"
console.log(typeof text); // "string"
console.log(typeof missing); // "undefined"
console.log(typeof empty); // "object"
console.log(typeof object); // "object"
console.log(typeof array); // "object"
console.log(typeof functionValue); // "function"

const a = { value: 1 };
const b = a;
const c = { value: 1 };

console.log(a === b); // true
console.log(a === c); // false

The first block creates several values and prints their types. Notice that null and arrays both report "object", while functions report "function". The second block shows reference behavior: b receives the same reference as a, so a === b is true. c is a different object with the same contents, so a === c is false.

Common mistakes

  • Assuming typeof null means null is an object. Fix: test value === null before treating it as an object.
  • Copying an object with =. Fix: create a new object with spread syntax, such as { ...original }, or use a deep copy when nested data must be independent.
  • Using == for type checks. Fix: prefer === to avoid coercion surprises.
  • Thinking arrays are a separate typeof result. Fix: use Array.isArray(value) for arrays.

When to use it

Use primitive types for simple values and reference types for structured data. The table below compares the closest alternatives.

KindExamplesComparisonBest for
Primitive42, "hi", trueBy valueFlags, IDs, text, numbers
Reference{}, [], functionsBy identityCollections, state, behavior

Practice

Guided exercise: Create one primitive, one object, and one array. Print each with typeof, then use Array.isArray on the array. Expected output includes "string", "object", "object", and true.

Challenge: Write code where two variables point to the same object, change one property, and prove the other variable sees the change. Hint: use const user = { name: "Sam" }; and const copy = user;.

Quick check

Question: What does typeof [] return, and what is a safer array check?

Answer: It returns "object". Use Array.isArray([]) to check for arrays.

Summary

JavaScript separates simple immutable primitives from mutable reference values. Primitives are copied and compared by value, while objects, arrays, and functions are copied by reference and compared by identity. Knowing this distinction prevents many common bugs.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JavaScript Data Types – FAQs

Quick answers about learning JavaScript Data Types in JavaScript.

This free note from Coding Now Tech Institute explains JavaScript Data Types in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including JavaScript Data Types, is 100% free with no signup required.
With focused practice, most students grasp JavaScript Data Types in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now