Skip to main content
⚑ OpenDevDocs
JavaScript
programmingjavascriptGuideBeginner

JavaScript Data Types & Type Coercion

Mastering JavaScript data types: 7 primitives (string, number, bigint, boolean, undefined, null, symbol), Objects, typeof operator, and type coercion.

Verified: 2026-09-30
Tested on:node 22.x
Platforms:
webnode
#javascript#data-types#primitives#coercion#types

JavaScript Data Types & Type Coercion

In JavaScript, every value is categorized as either a Primitive or an Object.


1. The 7 Primitive Types

Primitives are immutable values passed by value (copied).

TypeExampletypeof ResultNotes
String"OpenDevDocs", 'text', `template`"string"UTF-16 text sequence
Number42, 3.14, -0, NaN, Infinity"number"IEEE 754 64-bit float
BigInt9007199254740991n"bigint"Arbitrary-precision integers
Booleantrue, false"boolean"Logical flags
Undefinedundefined"undefined"Variable declared but unassigned
Nullnull"object" (legacy bug)Intentional absence of any value
SymbolSymbol("uniqueId")"symbol"Guaranteed globally unique identifier

2. Objects (Reference Types)

Objects, Arrays, Functions, Dates, and RegExps are Reference Types stored in heap memory. Assigning an object copies its memory reference, not the underlying data:

const obj1 = { name: "Alex" };
const obj2 = obj1; // Copies reference

obj2.name = "Sarah";
console.log(obj1.name); // "Sarah" (mutated same memory object!)

3. Explicit vs. Implicit Type Coercion

Implicit Coercion (Avoid)

"5" + 2;   // "52" (number coerced to string due to +)
"5" - 2;   // 3    (string coerced to number)
true + 1;  // 2    (true coerced to 1)

Explicit Conversion (Best Practice)

const str = "42";
const num = Number(str);         // 42
const bool = Boolean(str);       // true
const text = String(100);        // "100"

4. null vs. undefined

  • undefined: The variable has been declared, but has not yet been assigned a value (or a function returned nothing).
  • null: An explicit assignment made by the developer indicating "this value is intentionally empty".
let user;
console.log(user); // undefined

let activeSession = null; // explicitly cleared

On this page