Data Foundation

JavaScript Data Types

Data types define what kind of values variables can hold. JavaScript has 8 data types: 7 primitive types and objects. Understanding types is crucial for effective programming.

What are the data types in JavaScript?

JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null and symbol. Everything else (objects, arrays, functions, dates) is an object. Check a value's type with typeof, but note that typeof null is "object" (a historical bug) and you need Array.isArray() to detect arrays.

typeof "hi"         // "string"
typeof 42           // "number"
typeof 10n          // "bigint"
typeof true         // "boolean"
typeof undefined    // "undefined"
typeof null         // "object"   ← historical quirk
typeof {}           // "object"
Array.isArray([1])  // true

Primitive Data Types

Primitive types are the basic building blocks. They are immutable and stored by value. JavaScript has 7 primitive types:

String

Text data enclosed in quotes

"Hello World"'Single quotes'`Template literal`

Number

Integers and floating-point

423.14159-100

Boolean

True or false values

truefalse

Undefined

Variable declared but not assigned

undefined

Null

Intentional absence of value

null

Symbol

Unique identifier (ES6+)

Symbol("id")

BigInt

Large integers (ES2020)

123456789012345678901234567890n

null vs undefined

undefined means a variable was declared but not assigned. null is an explicit assignment of "no value". Both are falsy but different types.

Object Data Types

Objects are complex data types that can hold multiple values and functionality. They are mutable and stored by reference.

Object

Key-value pairs collection

{
  name: "Alice",
  age: 30,
  city: "New York"
}

Array

Ordered list of values

[1, 2, 3, "mixed", true]

Function

Reusable blocks of code

function greet(name) {
  return `Hello, ${name}!`;
}

Date

Date and time objects

new Date("2024-01-01")

Reference vs Value

Primitives are copied by value, objects are copied by reference. Modifying an object affects all references to it.

Interactive Data Types Example

Code Editor

<pre id="out"></pre>

Preview

Key Points in This Example:

  • • All primitive types with examples
  • • Object types including arrays and functions
  • • typeof operator for type checking
  • • Type conversion and checking methods

Type Checking & Conversion

Checking Types

typeof operator

typeof "hello" // "string"

Array.isArray()

Array.isArray([1, 2, 3]) // true

instanceof operator

obj instanceof Date // true/false

Type Conversion

To String

String(123) // "123"

To Number

Number("123") // 123

To Boolean

Boolean("hello") // true

Data types — step by step

1 / 4
1

Primitives vs objects — value vs reference

Primitives (string, number, boolean, null, undefined, symbol, bigint) are immutable values stored by value. Objects (arrays, functions, plain objects) are stored by reference. Assigning a primitive copies the value. Assigning an object copies the reference — both variables point to the same heap object. This is why mutating an array in a function affects the caller.

1 of 4
⚠️NaN is not equal to itself
NaN === NaN is false — NaN is the only value in JavaScript not equal to itself. Use Number.isNaN(value) to check for NaN (not the global isNaN(), which first coerces to a number). NaN arises from invalid arithmetic like 0/0, parseInt('abc'), or Math.sqrt(-1).

🎯 Quick Check

What does typeof null return in JavaScript?

JavaScript Data Types: summary

Key points

  • Primitives are copied by value. Objects are copied by reference, so two variables can point to the same object.
  • There's only one number type for integers and decimals; 0.1 + 0.2 is 0.30000000000000004.
  • undefined means "not assigned yet"; null means "intentionally empty".
  • NaN ("not a number") is a number value; test for it with Number.isNaN().

Frequently asked questions

›What is the difference between null and undefined?

undefined is what JavaScript gives you when something has no value: an unassigned variable, a missing property or a function that returns nothing. null is a value you assign on purpose to mean "empty".

›How do I check if a value is an array?

Use Array.isArray(value). typeof returns "object" for arrays.