JavaScript Data Types

JavaScript has primitive types (stored by value) and the object type (stored by reference). Understanding how values behave, how typeof reports them, and how conversion works will save you from bugs.

Summary of Data Types

Type Example Literal typeof result Notes
number 42, 3.14, NaN, Infinity "number" IEEE-754 double; NaN is a number; integers & floats share same type.
string 'hi', "hi", `hi` "string" Immut­able sequences of UTF-16 code units; use backticks for interpolation.
boolean true, false "boolean" Two values only; many expressions coerce to boolean in conditions.
null null "object" Intentional “empty” value. Historical quirk: typeof null is "object".
undefined undefined "undefined" Default for uninitialized variables or missing properties/returns.
symbol Symbol('id') "symbol" Unique identifiers; often used as object keys to avoid collisions.
bigint 9007199254740993n "bigint" Arbitrary-precision integers; can’t mix bigint and number in ops.
object {}, [], new Date(), functions "object" ("function" for functions) Collections and complex types, stored by reference; arrays are objects.

Note: Functions return "function" with typeof, but they are objects internally.

1) Numbers

One type for integers and floats. Be mindful of NaN and precision.


const a = 42, b = 3.14;
console.log(typeof a);           // "number"
console.log(0.1 + 0.2);        // 0.30000000000000004
console.log(Number.isNaN(NaN)); // true

2) Strings

Text data. Use backticks for interpolation and multiline.


const name = 'Sonu';
const msg = `Hello, ${name}!`;
console.log(msg); // Hello, Sonu!

3) Booleans

Logical values true or false. Often the result of comparisons.


const isAdult = 18 >= 18;  // true
if (isAdult) { /* ... */ }

4) null vs undefined

undefined means “not set”; null means “set to nothing on purpose”.


let x;                   // undefined
x = null;               // explicitly empty
console.log(typeof null); // "object" (historical quirk)

5) Symbol

Unique identifiers, useful for safe property keys.


const id = Symbol('id');
const obj = { [id]: 123 };
console.log(typeof id); // "symbol"

6) BigInt

For integers larger than Number.MAX_SAFE_INTEGER.


const big = 9007199254740993n;      // BigInt
// 9007199254740993 (number) cannot be represented precisely
console.log(typeof big); // "bigint"

7) Objects & Arrays

Reference types that can hold collections and complex structures.


const user = { name: 'Asha' };
const arr  = [1, 2, 3];

console.log(typeof user); // "object"
console.log(Array.isArray(arr)); // true

8) typeof & Common Quirks

Use typeof to check primitive types quickly; remember the special cases.


console.log(typeof null);      // "object"
console.log(typeof function(){}); // "function"
console.log(typeof NaN);       // "number"

9) Type Conversion (Explicit & Implicit)

Convert values deliberately; avoid relying on implicit coercion unless you know the rules.


// explicit
Number('42');     // 42
String(123);      // "123"
Boolean(0);       // false

// implicit (coercion)
'2' * 3;       // 6  (string->number)
'2' + 3;       // "23" (number->string)

10) Truthy & Falsy

In conditions, JS coerces values to boolean. These are falsy: 0, -0, NaN, '', null, undefined, false. Everything else is truthy.


if ('') { // falsy, won't run }
if ('0') { // truthy, runs (non-empty string) }
Tip: Prefer === over == to avoid unexpected coercion. For large integers use BigInt. Use Array.isArray() to detect arrays reliably.