JavaScript Data Types
JavaScript values fall into a small set of built-in data types that determine how a value behaves and what you can do with it.
What is a data type?
Every value in JavaScript has a type, and the type tells the engine how the value is stored in memory and which operations make sense for it. A number can be added or multiplied, a string can be joined with another string, and a boolean can steer an if statement. Because JavaScript is dynamically typed, you never declare the type of a variable up front. The same variable can hold a number one moment and a string the next, and the type is decided by whatever value it currently holds.
Primitive types
Primitive values are the simplest building blocks. They are immutable, meaning the value itself cannot be changed, and they are compared by their actual value. JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null, and symbol.
- string: text wrapped in single quotes, double quotes, or backticks, such as "hello".
- number: any numeric value, including integers and decimals like 42 or 3.14.
- bigint: whole numbers larger than the safe number range, written with an n suffix like 9007199254740993n.
- boolean: a logical value that is either true or false.
- undefined: the default value of a variable that has been declared but not assigned.
- null: an intentional empty value that a developer sets on purpose.
- symbol: a unique, hidden identifier often used as an object key.
Primitive values in action
<!DOCTYPE html>
<html>
<body>
<h2>Primitive values in action</h2>
<script>
let name = "Ada"; // string
let age = 36; // number
let isMentor = true; // boolean
let salary; // undefined (never assigned)
let manager = null; // null (empty on purpose)
console.log(typeof name); // "string"
console.log(typeof age); // "number"
console.log(typeof isMentor); // "boolean"
console.log(typeof salary); // "undefined"
</script>
</body>
</html>Objects: the reference type
Everything that is not a primitive is an object. Objects group related data and behavior together, and unlike primitives they are stored and compared by reference rather than by value. Arrays, functions, and dates are all specialized kinds of objects. When you copy an object into another variable, both variables point at the same underlying data.
Objects and arrays
<!DOCTYPE html>
<html>
<body>
<h2>Objects and arrays</h2>
<script>
const user = {
name: "Ravi",
role: "developer",
skills: ["JavaScript", "CSS"]
};
const scores = [90, 85, 72];
console.log(typeof user); // "object"
console.log(typeof scores); // "object"
console.log(Array.isArray(scores)); // true
</script>
</body>
</html>Exercise: JavaScript Data Types
What does the typeof operator return for a number like 42?