Lesson 30 of 296
The 7 primitive types and typeof
What you will learn
- What a data type is
- The 7 primitive types, each explained from zero
- What an object is
- The
typeofoperator and its two famous traps
On this page
- What is a data type?
- 1. string: text
- 2. number: numbers for maths
- 3. boolean: true or false
- 4. undefined: no value yet
- 5. null: empty on purpose
- 6. bigint: very large whole numbers
- 7. symbol: a unique identifier
- Object: everything else
- Summary table
- typeof: asking for the type
- The two famous traps
- Dynamic typing
- Common mistakes
- How to say it in an interview
- Key words
What is a data type?
Every value has a type. The type says what kind of data it is: text, a number, yes/no, and so on. The type decides what you can do with the value. You can multiply numbers, but you cannot multiply names.
JavaScript has 8 types: 7 primitive types and object.
A primitive is a simple, single value. It cannot be changed; you can only replace it with a new value.
1. string: text
Any text inside quotes is a string.
const name = "Rony";
const city = 'Huddersfield';
const phone = "07496261310";"07496261310" is a string, not a number, because it is in quotes. That is correct: you never do maths with a phone number.
2. number: numbers for maths
Whole numbers and decimals are both type number. JavaScript has no separate "integer" type.
const age = 30;
const price = 10.99;
const temperature = -3;number also has three special values: Infinity, -Infinity and NaN ("Not a Number", the result of maths that makes no sense, such as "abc" * 2).
3. boolean: true or false
A boolean has only two possible values: true or false. They are written in small letters, with no quotes.
const isLoggedIn = true;
const hasPaid = false;Booleans are used to make decisions: "if the user is logged in, show the dashboard".
4. undefined: no value yet
JavaScript gives undefined automatically when something has no value.
let result;
console.log(result); shows: undefinedThe variable exists, but nothing was put in it.
5. null: empty on purpose
null means "empty on purpose". You set it to show that there is intentionally no value.
let selectedProduct = null; nothing is selected yetundefined: JavaScript says "nothing has been set".null: the developer says "this is empty on purpose".
Lesson 43 covers the difference in detail.
6. bigint: very large whole numbers
A normal number can only store whole numbers exactly up to 9,007,199,254,740,991 (about 9 quadrillion). Above that, it starts making small mistakes. A bigint can be any size and stays exact. Write n at the end:
const big = 12345678901234567890n;You will rarely need it. Typical uses: very large IDs, or exact maths with huge numbers.
7. symbol: a unique identifier
A symbol is a value that is guaranteed to be unique. No two symbols are ever equal, even with the same description.
const id1 = Symbol("id");
const id2 = Symbol("id");
console.log(id1 === id2); falseSymbols are used inside libraries and the language itself. In everyday code you will rarely write one. Know the name for interviews.
Object: everything else
Anything that is not one of the 7 primitives is an object. An object can hold many values together.
const user = { name: "Sam", age: 30 }; an object
const colours = ["red", "green"]; an array (a special kind of object)
function greet() {} a function (also a kind of object)Summary table
| Type | Example | Meaning |
|---|---|---|
| string | "hello" | Text |
| number | 42, 3.5 | Numbers for maths |
| boolean | true, false | Yes or no |
| undefined | undefined | No value set yet |
| null | null | Empty on purpose |
| bigint | 10n | Very large whole numbers |
| symbol | Symbol("id") | A unique identifier |
| object | {}, [], functions | Collections of values |
typeof: asking for the type
typeof tells you the type of a value, as a string.
typeof "hi" "string"
typeof 42 "number"
typeof true "boolean"
typeof undefined "undefined"
typeof 10n "bigint"
typeof Symbol() "symbol"
typeof {} "object"The two famous traps
typeof null "object" (a bug from 1995, never fixed)
typeof [] "object" (arrays are objects)
typeof function () {} "function" (functions get their own answer)typeof nullsays "object" because of a mistake in the very first version of JavaScript. Fixing it now would break millions of old websites. To check for null, writevalue === null.- To check for an array, use
Array.isArray(value).
Array.isArray([1, 2]) true
Array.isArray({}) falseDynamic typing
In JavaScript, a variable has no fixed type; only the value inside it has one. The same let variable can hold a number now and a string later. This is called dynamic typing.
let data = 5; number
data = "five"; now a stringThis is flexible, but it causes bugs. TypeScript (Part 9) adds fixed types that are checked before the code runs.
Common mistakes
- Thinking
"42"and42are the same. One is text, one is a number. - Writing
TrueorFALSE. Booleans are always lowercase. - Using
typeof x === "object"to check for an object, which is also true fornulland arrays. - Saying "JavaScript has an int type" in an interview.
How to say it in an interview
"JavaScript has seven primitive types: string, number, boolean, undefined, null, bigint and symbol, plus object, which includes arrays and functions. Primitives are simple immutable values. typeof returns the type as a string, with two well-known quirks: typeof null is 'object', a historic bug, and typeof an array is 'object', so I use Array.isArray for arrays. JavaScript is dynamically typed: values have types, variables do not."
Key words
| Word | Meaning |
|---|---|
| Data type | The kind of data a value is |
| Primitive | A simple, single, unchangeable value |
| Object | A value that can hold many values together |
| typeof | An operator that returns a value's type as a string |
| Dynamic typing | Variables can hold values of any type, and the type can change |
| Immutable | Cannot be changed |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.