Skip to content

Beginner · First steps: values, variables and operators · topic 2 of 61 · 20 min

Values and types

Explanation

Every piece of data in JavaScript is a value, and every value has a type. There are seven primitive types (string, number, bigint, boolean, undefined, null and symbol) plus objects. The typeof operator tells you the type of a value.

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:

TypeExampleMeaning
string'Chennai', "OK"Text
number42, 3.14, -7Any number, whole or decimal
bigint9007199254740993nWhole numbers too big for number
booleantrue, falseYes or no
undefinedundefinedNo value has been given yet
nullnullEmpty on purpose
symbolSymbol('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

javascript
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'
typeof returns the type as a string.
Careful: typeof null gives "object". This is a mistake from the first version of JavaScript that can never be fixed without breaking old websites. To check for null, compare directly: value === null. To check for an array, use Array.isArray(value), because typeof [] is also "object".

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.

javascript
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.

javascript
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 object
Think of it like this: Copying a primitive is like photocopying an Aadhaar card: writing on the copy does not change the original. Copying an object is like sharing the address of a house: two people with the address visit the same house, so if one paints the door, the other sees it too.
Tip: Primitives are immutable: you cannot change a primitive value itself, only replace it. 'abc'.toUpperCase() makes a NEW string; the old one is untouched.

Need 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

Values and types

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.

javascript
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' 25
Convert text to a number before doing maths.

Practice (0/2 done)

Task 1 · Code · Javascript

Write a function describe(value) that returns the result of typeof for the value. Example: describe(5) returns "number".

Task 2 · Code · Javascript

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

Question 1

1.What does typeof null return?

Question 2

2.Which of these is NOT a primitive type?

Question 3

3.let city; console.log(city); What is printed?

Question 4

4.const x = { n: 1 }; const y = x; y.n = 5; What is x.n?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

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

Answer: 8

byte, short, int, long, float, double, char, boolean.

From Variables and primitive types

type Size = 'S' | 'M' | 'L'; const s: Size = 'XL'; What happens?Related

Answer: Compile error

'XL' is not one of the allowed literal types.

From Basic types and inference

What is typeof true?Related

Answer: 'boolean'

true and false are booleans.

From Values, types and variables

In SQLite, what does typeof(NULL) return?Related

Answer: null

typeof returns one of integer, real, text, blob or null.

From Tables, rows, columns and data types

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

  1. 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.
  2. Module 1 exam: First steps · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Strings, decisions and loops. 61 topics left in the course before the final exam.

← Previous: How JavaScript runs