Back to JavaScript Notes
Topic #350

Primitive Data Types

You will identify JavaScript primitive data types, understand that they are immutable values, and use typeof to distinguish them from objects.

What it is

Primitive data types are the simplest values in JavaScript. They are stored directly and cannot be changed in place. The primitive types are string, number, bigint, boolean, null, undefined, and symbol. When you “change” a primitive, JavaScript creates a new value and reassigns the variable. Related terms include value type, reference type, object, and wrapper object.

Why it matters

  • Primitives are the building blocks for text, numbers, flags, and missing values.
  • They are compared by value, so two equal primitives behave predictably.
  • Understanding immutability prevents bugs when passing values to functions.
  • typeof helps debug unexpected data shapes.
  • Knowing the null exception avoids incorrect type checks.

Syntax or steps

Declare a primitive with a literal value. Use const when the binding should not be reassigned, and let when it may change. Inspect the type with typeof.

const name = "Ada";
const age = 36;
const active = true;
const missing = undefined;
const empty = null;
const id = Symbol("id");
const big = 10n;

console.log(typeof name);
console.log(typeof age);
console.log(typeof active);
console.log(typeof missing);
console.log(typeof empty);
console.log(typeof id);
console.log(typeof big);

Example

let s = "str", n = 1, b = true, u = undefined;

console.log(typeof s); // "string"
console.log(typeof n); // "number"
console.log(typeof b); // "boolean"
console.log(typeof u); // "undefined"

let greeting = "Hello";
greeting = greeting + " world";
console.log(greeting); // "Hello world"

console.log(typeof null); // "object"

This example starts with four primitive variables: s is a string, n is a number, b is a boolean, and u is undefined. The typeof operator reports their primitive types. The greeting line shows immutability: the original string is not modified; a new string is created and assigned to greeting. The final line shows a special case: null is a primitive, but typeof null returns "object" because of an early JavaScript bug that was kept for compatibility.

Common mistakes

  • Confusing null and undefined. Use null for an intentional empty value and undefined for a missing value. Check with value === null or value === undefined.
  • Assuming typeof null === "object" means null is an object. Treat null as a primitive and test it separately.
  • Trying to mutate a primitive. Methods like toUpperCase() return a new string; assign the result if you need it.
  • Using new String() or new Number() accidentally. These create objects, not primitives. Prefer literals such as "text" and 42.

When to use it

Use primitives for simple values. Use objects or other reference types when you need mutable collections, functions, or structured data.

KindBest forBehavior
PrimitiveText, numbers, flags, unique IDs, missing valuesImmutable, compared by value
Object/referenceArrays, plain objects, functions, class instancesMutable, compared by reference

Practice

Guided exercise: Create variables for a product name, price, in-stock flag, and a missing description. Log each type.

const product = "Keyboard";
const price = 49.99;
const inStock = true;
const description = undefined;

console.log(typeof product);
console.log(typeof price);
console.log(typeof inStock);
console.log(typeof description);

Expected output: "string", "number", "boolean", "undefined".

Challenge: Write a function that returns true for primitives and false for objects and functions.

function isPrimitive(value) {
  return value === null || (typeof value !== "object" && typeof value !== "function");
}

Solution hint: remember that null is a primitive even though typeof null is "object".

Quick check

What does typeof null return, and is null a primitive?

It returns "object", but null is still a primitive type.

Summary

JavaScript primitives are simple, immutable values such as strings, numbers, booleans, null, undefined, symbols, and bigints. They are compared by value and are best for straightforward data. The main exception to remember is that typeof null returns "object" for historical reasons.

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

Primitive Data Types – FAQs

Quick answers about learning Primitive Data Types in JavaScript.

This free note from Coding Now Tech Institute explains Primitive 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 Primitive Data Types, is 100% free with no signup required.
With focused practice, most students grasp Primitive 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