search ]

JavaScript Data Types: Complete Guide with Examples

JavaScript is a dynamically typed language, meaning variables can hold different types of values at different times. Understanding data types is fundamental for writing effective code and avoiding common bugs.

In this guide, you’ll learn about every JavaScript data type, how to check types with typeof, the difference between null and undefined, and how JavaScript handles type coercion.

JavaScript has 7 primitive data types (String, Number, Boolean, Null, Undefined, Symbol, BigInt) and 1 structural type (Object). Arrays and functions are technically objects, but they behave differently and are often discussed as separate types.

TL;DR

The following table summarizes all JavaScript data types:

Data TypeDescriptionExample
StringRepresents textual datalet name = "John";
NumberRepresents numeric values (integer and floating-point)let age = 30;
BooleanRepresents logical values: true or falselet isActive = true;
NullRepresents an intentional absence of valuelet emptyValue = null;
UndefinedIndicates a variable has not been assigned a valuelet value;
SymbolRepresents a unique and immutable identifierlet sym = Symbol('id');
BigIntRepresents integers with arbitrary precisionlet big = 9007199254740993n;
ObjectRepresents collections of key-value pairslet person = { name: 'John' };
ArrayRepresents ordered collections of valueslet numbers = [1, 2, 3];
FunctionRepresents reusable blocks of codefunction greet() { return "Hello"; }

Primitive Data Types

Primitive data types are immutable values stored directly in the variable. When you assign a primitive to another variable, JavaScript copies the value. There are 7 primitive types:

1. String

Strings represent text and are created using single quotes, double quotes, or backticks (template literals):

let greeting = 'Hello, World!';
let name = "John Doe";
let message = `Hello, ${name}!`;

2. Number

Numbers represent both integer and floating-point values. JavaScript uses a single Number type for all numeric values:

let age = 30;
let pi = 3.14;
let largeNumber = 1e6; // 1000000

JavaScript also has special numeric values:

console.log(1 / 0);          // Infinity
console.log(-1 / 0);         // -Infinity
console.log('hello' * 2);    // NaN (Not a Number)
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991

NaN is a special value that indicates a failed numeric operation. It is unique in that NaN !== NaN – use Number.isNaN() to check for it.

3. Boolean

Booleans represent logical values: true or false.

let isActive = true;
let isComplete = false;

4. Null

Null represents an intentional absence of value. You explicitly assign null to indicate “no value”:

let emptyValue = null;
console.log(emptyValue); // null

5. Undefined

Undefined means a variable has been declared but not assigned a value:

let undefinedValue;
console.log(undefinedValue); // undefined

Null vs Undefined: Both represent “no value,” but they differ in intent. Use null when you intentionally want to set a variable to empty. undefined means the variable was never assigned. In practice, use null for your own code and treat undefined as JavaScript’s default “not yet set.”

6. Symbol

Symbols are unique and immutable identifiers, often used as object property keys to avoid naming collisions:

let symbol1 = Symbol('description');
let symbol2 = Symbol('description');
console.log(symbol1 === symbol2); // false (every Symbol is unique)

7. BigInt

BigInt represents integers larger than Number.MAX_SAFE_INTEGER (2^53 – 1). Create one by appending n to a number:

let bigIntValue = 9007199254740993n;
console.log(bigIntValue + 1n); // 9007199254740994n

BigInt cannot be mixed with regular numbers in arithmetic operations – you must convert one type first.

Object Data Types

Objects are reference types. Unlike primitives, when you assign an object to another variable, both variables point to the same data in memory.

1. Object

Objects are collections of key-value pairs:

let person = {
    name: 'John',
    age: 30
};
console.log(person.name); // John

2. Array

Arrays are ordered collections of values:

let numbers = [1, 2, 3, 4, 5];
console.log(numbers[0]); // 1

3. Function

Functions are reusable blocks of code:

function greet(name) {
    return `Hello, ${name}!`;
}
console.log(greet('Alice')); // Hello, Alice!

Checking Data Types with typeof

The typeof operator returns a string indicating the type of a value:

typeof 'hello'      // "string"
typeof 42           // "number"
typeof true         // "boolean"
typeof undefined    // "undefined"
typeof null         // "object" (this is a known JavaScript bug)
typeof Symbol('id') // "symbol"
typeof 10n          // "bigint"
typeof {}           // "object"
typeof []           // "object"
typeof function(){} // "function"

Notice that typeof null returns "object" – this is a historic bug in JavaScript that was never fixed for backward compatibility reasons. To check for null, use value === null.

To distinguish arrays from objects, use Array.isArray():

Array.isArray([1, 2, 3]); // true
Array.isArray({ a: 1 });  // false

Truthy and Falsy Values

Every JavaScript value has an inherent boolean “truthiness.” When used in a boolean context (like an if statement), values are coerced to either true or false.

Falsy values (evaluate to false):

false
0
-0
0n        // BigInt zero
""        // empty string
null
undefined
NaN

Everything else is truthy, including empty arrays [] and empty objects {}:

if ([]) console.log('empty array is truthy');   // logs
if ({}) console.log('empty object is truthy');   // logs
if ('0') console.log('string "0" is truthy');    // logs

Type Coercion

JavaScript automatically converts values between types when needed. This is called type coercion, and it can lead to unexpected results if you’re not aware of it:

console.log('5' + 3);   // "53" (number coerced to string)
console.log('5' - 3);   // 2   (string coerced to number)
console.log(true + 1);  // 2   (true coerced to 1)
console.log(null + 1);  // 1   (null coerced to 0)
console.log('' == 0);   // true (both coerced)

To avoid coercion-related bugs, use strict equality (===) instead of loose equality (==):

console.log(0 == '');    // true  (loose - type coercion happens)
console.log(0 === '');   // false (strict - different types)

FAQs

Common questions about JavaScript data types:

What is the difference between null and undefined in JavaScript?
Both represent "no value," but they differ in intent. undefined is JavaScript's default value for variables that have been declared but not assigned. null is a value you explicitly assign to indicate "intentionally empty." In practice, use null when you want to clear a variable, and treat undefined as "not yet set."
Why does typeof null return "object"?
This is a well-known bug from the very first version of JavaScript. In the original implementation, values were stored with a type tag, and null was represented as a null pointer (0x00), which shared the same tag as objects. The bug was never fixed because changing it would break existing code. To check for null, use value === null instead of typeof.
How do I check if a value is an array?
Use Array.isArray(value). The typeof operator returns "object" for both arrays and regular objects, so it cannot distinguish between them. Array.isArray([1, 2]) returns true, while Array.isArray({ a: 1 }) returns false.
What is the difference between == and === in JavaScript?
The == operator (loose equality) converts both values to the same type before comparing, which can cause unexpected results like 0 == "" being true. The === operator (strict equality) compares both value and type without conversion, so 0 === "" is false. Always prefer === unless you specifically need type coercion.
When should I use BigInt instead of Number?
Use BigInt when you need to work with integers larger than Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). Common use cases include working with database IDs, cryptographic values, or high-precision timestamps. Note that BigInt cannot be mixed with regular Number values in arithmetic - you must convert one type first.

Summary

JavaScript has 7 primitive types (String, Number, Boolean, Null, Undefined, Symbol, BigInt) and 1 reference type (Object). Primitives are immutable and copied by value, while objects are copied by reference.

Use the typeof operator to check data types, but remember its quirks: typeof null returns "object" and typeof [] also returns "object". Use Array.isArray() and === null for accurate checks.

Understanding type coercion and truthy/falsy values will help you avoid common bugs. Always use strict equality (===) to prevent unexpected type conversions.

Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo