Last modified: May 13, 2026 By Alexander Williams
JavaScript Variable Types Guide
JavaScript uses variables to store data. Each variable holds a specific type of value. Understanding these types is essential for writing clean code. This guide explains all JavaScript variable types with simple examples.
Variables are like labeled boxes. You put data inside them. The type tells you what kind of data is stored. JavaScript is dynamically typed. This means a variable can change its type at runtime.
Primitive Variable Types
Primitive types are the most basic data types. They are immutable. This means their value cannot be changed once created. There are seven primitive types in JavaScript.
1. Number
The number type stores both integers and floating-point numbers. JavaScript uses 64-bit double precision for numbers. This includes special values like Infinity and NaN (Not a Number).
// Number examples
let age = 25; // integer
let price = 19.99; // floating-point
let infinity = 1 / 0; // Infinity
let notNumber = "abc" * 2; // NaN
console.log(age); // 25
console.log(infinity); // Infinity
console.log(notNumber); // NaN
25
Infinity
NaN
2. String
The string type stores text. You can use single quotes, double quotes, or backticks. Backticks allow template literals for embedding expressions.
// String examples
let name = "Alice"; // double quotes
let greeting = 'Hello'; // single quotes
let template = `Hi, ${name}`; // backticks with template
console.log(greeting); // Hello
console.log(template); // Hi, Alice
Hello
Hi, Alice
3. Boolean
The boolean type has only two values: true or false. It is used for logical operations and conditional statements.
// Boolean examples
let isActive = true;
let isComplete = false;
let result = 10 > 5; // true
console.log(result); // true
true
4. Undefined
The undefined type means a variable has been declared but not assigned a value. It is the default value for uninitialized variables.
// Undefined example
let myVariable;
console.log(myVariable); // undefined
let anotherVar = undefined;
console.log(anotherVar); // undefined
undefined
undefined
5. Null
The null type represents an intentional absence of any object value. It is often used to reset or clear a variable.
// Null example
let emptyValue = null;
console.log(emptyValue); // null
// Note: typeof null returns "object" (a known JavaScript bug)
console.log(typeof null); // object
null
object
6. Symbol
The symbol type creates unique identifiers. Each symbol is guaranteed to be unique. They are often used as object property keys.
// Symbol example
let sym1 = Symbol("id");
let sym2 = Symbol("id");
console.log(sym1 === sym2); // false (unique values)
console.log(typeof sym1); // symbol
false
symbol
7. BigInt
The bigint type stores integers larger than the Number type limit. Append n to the end of an integer to create a BigInt.
// BigInt example
let bigNumber = 123456789012345678901234567890n;
console.log(bigNumber); // 123456789012345678901234567890n
console.log(typeof bigNumber); // bigint
123456789012345678901234567890n
bigint
Reference Variable Types
Reference types store references to objects. They are mutable. The most common reference type is Object. Arrays and functions are also objects.
Object
The object type stores collections of key-value pairs. Keys are strings or symbols. Values can be any type.
// Object example
let person = {
name: "Bob",
age: 30,
isEmployed: true
};
console.log(person.name); // Bob
console.log(person.age); // 30
Bob
30
Array
An array is an ordered list of values. Arrays are objects in JavaScript. They use zero-based indexing.
// Array example
let fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // apple
console.log(fruits.length); // 3
// Arrays can hold mixed types
let mixed = [1, "hello", true];
console.log(mixed[2]); // true
apple
3
true
Function
Functions are also objects. They can be stored in variables. The function type is callable.
// Function example
let greet = function(name) {
return "Hello, " + name;
};
console.log(greet("Alice")); // Hello, Alice
console.log(typeof greet); // function
Hello, Alice
function
Type Checking
Use the typeof operator to check a variable's type. It returns a string indicating the type. Be careful with null and arrays.
// Type checking examples
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (bug)
console.log(typeof []); // "object"
console.log(typeof {}); // "object"
console.log(typeof function(){}); // "function"
number
string
boolean
undefined
object
object
object
function
For arrays, use Array.isArray() to check. For null, compare directly with ===.
// Better type checking
let arr = [1, 2, 3];
console.log(Array.isArray(arr)); // true
let empty = null;
console.log(empty === null); // true
true
true
Type Coercion
JavaScript automatically converts types when needed. This is called type coercion. It can lead to unexpected results. Always use strict equality === to avoid coercion issues.
// Type coercion examples
console.log("5" - 2); // 3 (string to number)
console.log("5" + 2); // "52" (number to string)
console.log(5 == "5"); // true (loose equality)
console.log(5 === "5"); // false (strict equality)
3
52
true
false
Best Practices
Always declare variables with let or const. Avoid var due to scoping issues. Use const by default and let only when you need to reassign. For more details, read our JavaScript Variable Declaration Guide.
Initialize variables when you declare them. This prevents undefined values. Check types explicitly with typeof or Array.isArray() before operations.
Understand that primitive types are passed by value. Reference types are passed by reference. This affects how variables behave in functions. For a deeper dive, explore our JavaScript Variables Guide.
Conclusion
JavaScript has seven primitive types: number, string, boolean, undefined, null, symbol, and bigint. Reference types include objects, arrays, and functions. Use typeof for type checking, but remember its quirks. Always prefer strict equality and proper initialization. Mastering variable types helps you write predictable and bug-free code. Practice with examples and explore more resources like the JavaScript Variable Declaration Guide to deepen your understanding.