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]) // truePrimitive 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-100Boolean
True or false values
truefalseUndefined
Variable declared but not assigned
undefinedNull
Intentional absence of value
nullSymbol
Unique identifier (ES6+)
Symbol("id")BigInt
Large integers (ES2020)
123456789012345678901234567890nnull 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]) // trueinstanceof operator
obj instanceof Date // true/falseType Conversion
To String
String(123) // "123"To Number
Number("123") // 123To Boolean
Boolean("hello") // trueData types — step by step
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.
🎯 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
numbertype for integers and decimals;0.1 + 0.2is0.30000000000000004. undefinedmeans "not assigned yet";nullmeans "intentionally empty".NaN("not a number") is a number value; test for it withNumber.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.