01 / 22
{ }

JavaScript
Types

Master the six fundamental data types in JavaScript: number, string, boolean, null, undefined, and object: the building blocks of every program.

JavaScript

What Are Data Types?

Every value in JavaScript has a type. A type tells the engine what kind of data you are working with and what operations are valid on it. Think of types as labels on containers: the label determines what you can put inside and what you can do with the contents.

Discovering Types with typeof

JavaScript provides the typeof operator to inspect a value's type at runtime. It returns a string describing the type.

typeof 42; // "number" typeof "hello"; // "string" typeof true; // "boolean" typeof undefined; // "undefined" typeof null; // "object" (quirk!) typeof {}; // "object"

Why Types Matter

1
Correct operations: You can add numbers but not divide strings. Types prevent nonsensical operations.
2
Bug prevention: Type-related bugs cause roughly 15% of all JavaScript runtime errors. Understanding types helps you avoid them.
3
Implicit coercion: JavaScript silently converts types in some expressions. Knowing the rules means no surprises.

The JavaScript Type Family

JavaScript Values Primitives Objects number string boolean null undefined { } Plain [ ] Array fn( ) 5 Primitive Types + 1 Object Type (Symbol & BigInt exist but are advanced topics)

Primitives vs Objects

Primitives Objects
Examples 42, "hi", true {}, [], function
Mutability Immutable Mutable
Compared by Value Reference
typeof Returns own type Returns "object"
Can hold properties? No Yes

The Number Type

JavaScript has one number type for both integers and decimals. Under the hood, all numbers are 64-bit floating point (IEEE 754).

let age = 25; // integer let price = 9.99; // decimal let negative = -10; // negative let big = 1e6; // 1,000,000 let oops = 0 / 0; // NaN let huge = 1 / 0; // Infinity
NaN
"Not a Number": result of invalid math. Oddly, NaN !== NaN is true.
Infinity
Represents mathematical infinity. Both Infinity and -Infinity exist.

The String Type

Strings represent text. They can be created with single quotes, double quotes, or backticks (template literals).

let a = 'single quotes'; let b = "double quotes"; let name = "Ada"; let c = `Hello, ${name}!`; // template literal // Strings are immutable: let word = "cat"; word[0] = "b"; // no effect! console.log(word); // "cat"

The Boolean Type

Booleans have exactly two values: true and false. They are the backbone of every if statement, loop, and conditional expression.

let isLoggedIn = true; let hasAccess = false; // Comparison operators return booleans: 5 > 3; // true 10 === "10"; // false (strict equality) 10 == "10"; // true (loose equality, coercion!)

null vs undefined

null
Intentional absence of a value. You, the programmer, assign null to say "this is deliberately empty."
undefined
Unintentional absence. JavaScript gives this when a variable is declared but never assigned, or a function has no return.
let x; // undefined (no assignment) let y = null; // null (intentional) null == undefined; // true (loose) null === undefined; // false (strict)

The Object Type

Objects store collections of key-value pairs. Arrays and functions are specialized objects. Objects are mutable and compared by reference.

let user = { name: "Ada", age: 30, isAdmin: false }; console.log(user.name); // "Ada" console.log(user["age"]); // 30 // Mutation is allowed: user.age = 31; // modifies the object

How to Check Types

1
Use typeof for primitives: typeof x returns "number", "string", "boolean", or "undefined" reliably.
2
Check for null explicitly: Since typeof null is "object", use x === null to test for null.
3
Use Array.isArray() for arrays: typeof [] returns "object". Use Array.isArray(x) to distinguish arrays from plain objects.
4
Use typeof for functions: typeof myFunc returns "function": a bonus typeof result even though functions are technically objects.

Example: Identifying Types

For each value, predict the result of typeof:

typeof 3.14 // ? typeof "3.14" // ? typeof false // ? typeof [1, 2, 3] // ? typeof null // ? typeof undefined // ?

Example: Type Coercion

JavaScript silently converts types in mixed expressions. Predict each result and its type:

"5" + 3 // "53" (string) "5" - 3 // 2 (number) true + 1 // 2 (number) false + "!" // "false!" (string) null + 5 // 5 (number: null → 0) undefined + 5 // NaN (undefined → NaN)

Example: Value vs Reference

// Primitives: copied by value let a = 10; let b = a; b = 20; console.log(a); // 10 (unchanged) // Objects: copied by reference let obj1 = { score: 100 }; let obj2 = obj1; obj2.score = 200; console.log(obj1.score); // 200 (changed!)
Primitives (by value) a = 10 b = 20 Separate copies Objects (by reference) obj1 obj2 {score: 200}

Common Mistakes

✕ Using == instead of ===
Loose equality coerces types: "0" == false is true. Always use === (strict equality) to avoid surprises.
✕ Checking null with typeof
typeof null returns "object", not "null". Use x === null to test for null correctly.
✕ Assuming NaN === NaN
NaN is not equal to anything, including itself. Use Number.isNaN(x) or isNaN(x) to detect it.

Type Summary

Type typeof Result Example Falsy?
number "number" 42, 3.14, NaN 0, NaN
string "string" "hello", "" ""
boolean "boolean" true, false false
null "object" null null
undefined "undefined" undefined undefined
object "object" {}, [] Never

Question 1 of 5

What does typeof "42" return?

Question 2 of 5

What is the result of "5" + 3 in JavaScript?

Question 3 of 5

Which of the following correctly checks if a variable x is null?

Question 4 of 5

What is the difference between null and undefined?

Question 5 of 5

Given the code below, what does console.log(obj1.val) print?