Beginner · First steps: values, variables and operators · topic 2 of 61 · 20 min
Values and types
Explanation
Why types matter
A value is one piece of data: 42, 'Pune', true. A type is the kind of value it is. The type decides what you can do with it: you can multiply numbers, make text uppercase, and check whether something is true. Mixing up types is one of the most common sources of bugs in JavaScript.
The primitive types
A primitive is a simple value that is not an object. JavaScript has seven of them:
| Type | Example | Meaning |
|---|---|---|
| string | 'Chennai', "OK" | Text |
| number | 42, 3.14, -7 | Any number, whole or decimal |
| bigint | 9007199254740993n | Whole numbers too big for number |
| boolean | true, false | Yes or no |
| undefined | undefined | No value has been given yet |
| null | null | Empty on purpose |
| symbol | Symbol('id') | A unique label, used for special keys |
Everything else is an object: arrays, functions, dates and plain objects like { name: 'Asha' }. You will learn objects in module 4.
typeof: asking for the type
console.log(typeof 'Asha'); // 'string'
console.log(typeof 250); // 'number'
console.log(typeof 10n); // 'bigint'
console.log(typeof true); // 'boolean'
console.log(typeof undefined); // 'undefined'
console.log(typeof null); // 'object' (an old bug!)
console.log(typeof [1, 2]); // 'object'
console.log(typeof function () {}); // 'function'undefined versus null
undefined means "nothing has been put here yet". A variable you declared but did not fill, a missing object property, and a function with no return all give undefined. null means "empty on purpose": you set it yourself, for example when a user has no profile photo.
let city;
console.log(city); // undefined (declared, never filled)
const user = { name: 'Ravi', photo: null };
console.log(user.photo); // null (empty on purpose)
console.log(user.age); // undefined (no such property)Primitives are copied, objects are shared
When you copy a primitive into another variable, you get an independent copy. When you copy an object, both names point to the same object. This difference explains many surprising bugs and is covered fully in module 4.
let a = 10;
let b = a;
b = 20;
console.log(a); // 10, a did not change
const x = { score: 10 };
const y = x;
y.score = 20;
console.log(x.score); // 20, x and y are the same objectNeed help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Values and types · 1/6
In this lesson you will meet the different kinds of values in JavaScript, which are called types.
Prefer a video by a teacher? Find free videos on “javascript data types primitives typeof explained” on YouTube ↗
In real life: Form data from a sign-up page
On a job portal, everything a user types into a form arrives as a string, even the age "24". Before saving, the backend checks types: is age really a number, is the phone a string of 10 digits, is the "accepted terms" field a boolean? Many production bugs come from storing "24" (text) where 24 (number) was expected.
const form = { name: 'Meena', age: '24', acceptedTerms: true };
console.log(typeof form.age); // 'string'
const age = Number(form.age);
console.log(typeof age, age + 1); // 'number' 25Practice (0/2 done)
Write a function describe(value) that returns the result of typeof for the value. Example: describe(5) returns "number".
typeof is not good enough for null and arrays. Write a function realType(value) that returns "null" for null, "array" for arrays, and typeof value for everything else.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Variables: let, const and varFirst steps: values, variables and operators · same module
- Operators and type coercionFirst steps: values, variables and operators · same module
- Variables and primitive typesJava Programming (Core to Spring Boot) · Getting started with Java
- Basic types and inferenceTypeScript basics
- Values, types and variablesFrontend Web Development · JavaScript basics
- Tables, rows, columns and data typesSQL and Databases · Databases and the relational model
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does typeof null return?
Answer: "object"
This is a famous historical bug: typeof null is "object".
Which of these is NOT a primitive type?
Answer: array
Arrays are objects. The primitives are string, number, bigint, boolean, undefined, null and symbol.
let city; console.log(city); What is printed?
Answer: undefined
A declared variable with no value holds undefined.
const x = { n: 1 }; const y = x; y.n = 5; What is x.n?
Answer: 5
x and y point to the same object, so the change through y is seen through x.
How many primitive types does Java have?Related
type Size = 'S' | 'M' | 'L'; const s: Size = 'XL'; What happens?Related
What is typeof true?Related
In SQLite, what does typeof(NULL) return?Related
Answer: null
typeof returns one of integer, real, text, blob or null.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “First steps: values, variables and operators” before its exam: Variables: let, const and var, Operators and type coercion, Numbers and Math.
- Module 1 exam: First steps · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Strings, decisions and loops. 61 topics left in the course before the final exam.