Skip to content
Theory · Variables and types · lesson 3 of 9

Primitive types

The boxes from the previous lesson hold different items, and each is handled in its own way. A book is read, and an apple is eaten. Values in a program are the same: two numbers can be added, and two words can be joined end to end, as "sun" and "flower" give "sunflower".

The same + sign works differently depending on what stands on its sides:

JavaScript
console.log(2 + 2);  // 4
console.log("2" + "2");  // 22

Let's go through both lines:

  • 2 + 2 — two numbers, and + adds them: the result is 4;
  • "2" + "2" — two strings, and + glues them into one: the result is 22.

The values 2 and "2" look alike, yet they are of different kinds, and the kind decides what + does with them.

Seven primitive types

The kind of a value is called its type. JavaScript has seven primitive types — the types of simple values that are not made of other values.

  • number — numbers, whole and fractional: 42, -7, 3.14.
  • string — strings, text in quotes: "Hi", "2".
  • boolean — logical values, of which there are only two: true and false.
  • undefined and null — two values that mean "no value".
  • bigint and symbol — very large whole numbers and special unique values; early on they hardly ever come up.

A string can also be written in single quotes, '2', but this course writes strings in double quotes.

A type belongs to the value

A type belongs to a value, not to a variable. A variable declared with let can hold a number first and, after an assignment, a string.

Back to the storeroom. A let box is not made for one kind of item: today it holds a number, tomorrow a string. The type is told by what lies inside, not by the box itself.

The typeof operator

A sign or word that computes a new value from the values next to it is called an operator. Both + and typeof are operators. The typeof operator returns the name of a value's type as a string.

console.log takes several values separated by commas and prints them side by side, separated by spaces:

JavaScript
const name = "Ann";
console.log(typeof 42, typeof name);  // number string
console.log(typeof true, typeof undefined);  // boolean undefined
console.log(typeof 3.14, typeof NaN);  // number number

Let's go through the output line by line:

  1. typeof 42 gives number. For typeof name, JavaScript looks at the value in the variable, "Ann", and gives string.
  2. true is a boolean, and the type of undefined has the same name as the value itself.
  3. Both 3.14 and NaN give number.

The last line shows two quirks:

  • fractional numbers have no type of their own: 3.14 is a number too;
  • NaN (not a number) is a special value of type number that means "not a number".

The typeof null quirk

The typeof operator gives an unexpected answer for null:

JavaScript
console.log(typeof null);  // object

This is an old mistake in the language: null is a primitive value, but typeof calls it "object". That is the name of the type of compound values, and null is not one of them. The mistake cannot be fixed without breaking programs already written, so the answer "object" for null has to be memorized.

Interviewers often ask what typeof null returns. The right answer is "object", and it is worth adding that this is an old mistake in the language, not the real type of null.

Common mistake

A string was written without quotes:

JavaScript
const greeting = hello;  // ✗
console.log(greeting);
TEXT
ReferenceError: hello is not defined

Without quotes, hello is a variable name. JavaScript looks for a variable with that name, does not find it and stops the program on this line. Text becomes a value of type string only in quotes: const greeting = "hello";.

Key points

  • A type is the kind of a value; the type decides what an operator such as + does with the value.
  • There are seven primitive types: number, string, boolean, undefined, null, bigint and symbol.
  • A type belongs to a value, not to a variable: a let can hold a number first and a string later.
  • typeof returns the type's name as a string: typeof NaN is "number", and typeof null is "object".

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way