Data Types — JavaScript

A data type is the kind of a value. A string is text inside quote marks, a number has no quote marks, a boolean is true or false. An array is a list of values…

A stall's online checkout adds a price of $15 to a delivery fee of $2. You expect $17. But if the price is stored as text ("15", in quote marks), JavaScript joins the two values into "152". Both cases use the same +. The only difference is the kind of value: a number or text.

The kind of a value is called its data type. Examples: text, a number, a yes or no, a list. The data type (often just called the type) decides what you can do with a value: you can add numbers, and you can join text.

Three basic types: string, number, boolean

This code makes three variables with different types, then prints each type:

let city = "Leeds";
let price = 15;
let hungry = true;

console.log(typeof city);  // "string"
console.log(typeof price); // "number"
console.log(typeof hungry); // "boolean"

Quote marks change the type. 42 is a number, "42" is a string. So + gives different results: 42 + 1 gives 43, while "42" + 1 gives "421". When one of the values is a string, + joins instead of adding. In the same way, "true" in quote marks is a string, not a boolean.

There are two more basic types that beginners rarely use. bigint is for very large whole numbers, written with n at the end (10n). symbol is for names that are sure to be unique (never the same as any other name).

Arrays and objects: groups of values

The next two types hold several values together:

let shopping = ["rice", 2, true];
let position = { x: 3, y: 7 };

Before you read on, guess: what does typeof [1, 2, 3] give?

The answer is "object", not "array". In JavaScript, an array is a kind of object. To be sure a value is an array, use Array.isArray(shopping). It gives true.

Empty: undefined and null

Two values mean empty. This code uses both:

let address;
let photo = null;

console.log(address); // undefined
console.log(typeof photo); // "object"
console.log(photo === null); // true

⚠️ Misplaced quotes and a misleading typeof

TL;DR: A data type is the kind of a value. A string is text inside quote marks, a number has no quote marks, a boolean is true or false. An array is a list of values inside [ ], an object is a group of name and value pairs inside { }. undefined means no value has been given yet, null means emptied on purpose. typeof before a value gives the name of its type as text. But for an array and for null, typeof gives "object" both times. So check for an array with Array.isArray(), and check for null with === null (=== means exactly the same).

⚡ String, number, boolean

Write:

let city = "Leeds";
let price = 15;
let hungry = true;

console.log(typeof price); // "number"

Meaning: Every value has a data type, which is its kind. A string is text inside quote marks, a number has no quote marks, and a boolean is only true or false. typeof price gives the type of what price holds, as text: here "number".

⚡ Quote marks change the type

Guess first: what does "42" + 1 give?

"421". "42" in quote marks is a string, and + joins strings instead of adding. Without quote marks, 42 + 1 gives 43.

Meaning: The type comes from the value, not from the variable's name. A value in quote marks is always a string: "true" is a string, not a boolean.

⚡ An array says it's an object

Write:

let shopping = ["rice", 2, true];
console.log(Array.isArray(shopping)); // true
Guess first: what does typeof [1, 2, 3] give?

"object", not "array". An object is the type for a group of values, and in JavaScript an array is a kind of object.

Meaning: An array is a list of values inside [ ]. To be sure a value is an array, use Array.isArray(). typeof null also gives "object" (null is a value that means emptied on purpose). So check for null directly: photo === null for a variable named photo. The === sign checks whether two values are exactly the same.

What you will learn