Data Types in JavaScript
Introduction
Every value in JavaScript has a type—number, string, boolean, object, and more. This chapter explains the main types beginners use daily, how to inspect them with typeof, and how dynamic typing affects real programs. Type clarity prevents bugs such as comparing strings when you meant numbers.
Prerequisites
Dynamic Typing
The same variable can hold different types over time (usually avoid doing this without reason):
// Dynamic typing example (educational, not a daily habit)
let value = 42;
value = "forty-two";
value = true;
console.log(value);In production code, keep each variable’s meaning stable even though the language allows changes.
Primitive Types (Overview)
| Type | Example | Typical use |
|---|---|---|
| string | "hello" | Text, messages, IDs |
| number | 42, 3.14 | Counts, money, scores |
| boolean | true, false | Flags, conditions |
| undefined | undefined | Missing value |
| null | null | Intentional empty value |
| bigint | 9007199254740991n | Very large integers |
| symbol | Symbol("id") | Unique keys (advanced) |
Objects (including arrays and functions) are not primitives—you will study them soon.
Strings
Text in single quotes, double quotes, or backticks:
// String literals
const city = "Austin";
const greeting = 'Hello';
const message = `Welcome to ${city}`;
console.log(message);Numbers
All numbers are IEEE 754 doubles—integers and decimals share one type:
// Integer and decimal are both number
const count = 10;
const ratio = 0.75;
const total = count * ratio;
console.log(total);Booleans
Results of comparisons and logical operations:
// Boolean from comparison
const age = 20;
const canVote = age >= 18;
console.log(canVote);undefined and null
undefined: not assigned, missing property, or no return valuenull: deliberate “no value” chosen by the developer
// undefined vs null
let nickname;
console.log(nickname);
const selectedUser = null;
console.log(selectedUser);Use === null or === undefined when you need strict checks (covered with operators later).
typeof Operator
Inspect a value’s type at runtime:
// typeof examples
console.log(typeof "hi");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null);
console.log(typeof { a: 1 });Warning
typeof null returns "object"—a long-standing quirk. For null checks, use value === null instead of relying on typeof.
Objects and Arrays (Preview)
Objects group named properties; arrays are ordered lists—both are object types under the hood:
// Object and array literals
const user = { id: 1, name: "Lin" };
const scores = [90, 85, 88];
console.log(typeof user);
console.log(Array.isArray(scores));Dedicated chapters explore objects and arrays in depth.
Type Coercion (Preview)
JavaScript sometimes converts types automatically:
// String + number concatenates
console.log("5" + 2);
// Subtraction coerces to number
console.log("5" - 2);A dedicated type conversion chapter comes later with safer patterns.
Mini Example: Profile Card
// Mix types in a small profile object
const username = "dev_ada";
const yearsActive = 3;
const isVerified = true;
const bio = null;
const profile = {
username,
yearsActive,
isVerified,
bio,
};
console.log(typeof profile.username);
console.log(profile);FAQ
How many types does JavaScript have?
Commonly seven primitives plus objects. Arrays, functions, dates, and maps are specialized objects.
Is JavaScript strongly or weakly typed?
Often described as dynamically typed with coercion—types are attached to values, not variable declarations, and the engine may convert types in expressions.
Should I use Number or parseInt for user input?
User input arrives as strings. Parse explicitly when you need numbers—covered with input and operators in later chapters.
What is NaN?
“Not a Number” from invalid numeric operations. typeof NaN is "number".
What comes next?
Scope, strings, numbers, and operators in the following chapters.