Sub topic 1
The Eight Types: Seven Primitives and Object
Every value in JavaScript belongs to exactly one of eight types. Seven of them are primitives, which are simple, immutable values. The eighth is object, which holds collections of properties. The rest of this tutorial builds on this split. Coercion rules, equality checks, and typeof results all depend on which side of it a value falls.
Prerequisites
This chapter assumes you can:
- Declare variables with
letandconstand write basic functions. - Create and read object literals and arrays.
- Run snippets in a browser console or with Node.js.
The Seven Primitives
| Type | Example literals | What it represents |
|---|---|---|
| string | `"hi"`, `'a'`, `` `x` `` | A sequence of UTF-16 code units |
| number | `42`, `3.14`, `NaN`, `Infinity` | A 64-bit floating-point value |
| bigint | `10n`, `2n ** 64n` | An integer of any size |
| boolean | `true`, `false` | A logical truth value |
| undefined | `undefined` | A value that was never assigned |
| null | `null` | A deliberate "no value" |
| symbol | `Symbol("id")` | A unique identifier, often used as a property key |
Primitives are immutable. You cannot change a primitive value in place. Operations that look like they modify a string actually return a new one:
"use strict";
let greeting = "hello";
const shouted = greeting.toUpperCase();
console.log(greeting); // "hello": the original is unchanged
console.log(shouted); // "HELLO": a new string
try {
greeting[0] = "J"; // strings cannot be modified in place
} catch (err) {
console.log(err instanceof TypeError); // true in strict mode
}In sloppy (non-strict) mode, that assignment fails silently instead of throwing. Either way, greeting stays "hello".
The Object Type
Everything that is not a primitive is an object. Arrays, functions, dates, regular expressions, maps, and plain {} literals are all objects. They differ in their built-in behavior, but they share one type.
const things = [
{},
[1, 2, 3],
function () {},
new Date(),
/abc/,
new Map()
];
console.log(things.every(v => v instanceof Object)); // trueObjects are mutable, and variables hold references to them, not copies. The next chapter explains what that means for assignment and comparison.
Common Misconceptions
- "Arrays and functions are separate types." They are both objects with special behavior.
- "null is an object." It is a primitive. The confusion comes from a
typeofquirk covered later. - "Integers and decimals are different types." Both are
number. Onlybigintis a separate integer type.
Exercise
Classify each value as a primitive (and name its type) or an object: "42", 42n, null, [], Symbol(), () => 1, undefined.
Show answer
"42" is a string, 42n is a bigint, null is null, Symbol() is a symbol, and undefined is undefined. All five are primitives. [] and () => 1 are objects (an array and a function).
Exercise
After let s = "cat"; s.toUpperCase();, what is the value of s, and why?
Show answer
It is still "cat". Strings are immutable, so toUpperCase returns a new string "CAT". The result was never assigned, so it is discarded.