Skip to content

Variables & Datatypes

A variable is a name for a value, so the program can store the value once and refer to it later. Modern JavaScript declares variables with const and let:

  • const declares a variable that cannot be given a new value.
  • let declares a variable that can be.

Use const by default, and let only for a value that has to change, such as a running total. An older keyword, var, still works, but it has scoping rules that cause surprises; Variable Declarations covers the differences.

const shop = 'Corner Store';
let itemsInBasket = 3;
let total = 7.5;
let paid = false;
itemsInBasket = itemsInBasket + 1;
total = total + 2.25;
console.log(shop, itemsInBasket, total, paid);

This prints:

Corner Store 4 9.75 false

= is assignment: it works out the value on its right and stores it in the variable on its left, which is why itemsInBasket = itemsInBasket + 1 adds one to the old value.

Giving a const a new value is an error:

const vatRate = 0.2;
try {
vatRate = 0.15;
} catch (error) {
console.log(error.name + ': ' + error.message);
}

This prints:

TypeError: Assignment to constant variable.

JavaScript values have a type, but variables do not, so the same let variable can hold a number now and a string later. There are seven primitive types, which are simple values that cannot be changed in place:

Type Example Used for
string 'apples' text
number 42, 3.5 integers and decimals alike
boolean true, false yes or no
undefined undefined a variable that has not been given a value
null null a deliberate “no value”
bigint 9007199254740993n whole numbers too large for a number
symbol Symbol('id') unique property keys

The typeof operator reports a value’s type as a string:

let discountCode;
const deliveryNote = null;
console.log(typeof 'apples');
console.log(typeof 42);
console.log(typeof 3.5);
console.log(typeof true);
console.log(typeof discountCode);
console.log(typeof deliveryNote);
console.log(typeof 9007199254740993n);
console.log(typeof Symbol('id'));

This prints:

string
number
number
boolean
undefined
object
bigint
symbol

typeof null gives 'object', which is a long-standing mistake in the language that cannot be fixed without breaking old code. To test for null, compare with value === null. Null vs Undefined explains when each one appears.

Everything that is not a primitive is an object: a collection of values that can be changed. Arrays, plain objects and functions are the ones met first:

const basket = ['apples', 'bread', 'cheese'];
const customer = { name: 'Priya', loyaltyPoints: 120 };
const addTax = (price) => price * 1.2;
console.log(basket.length, basket[0]);
console.log(customer.name, customer.loyaltyPoints);
console.log(addTax(10));
console.log(typeof basket, typeof customer, typeof addTax);
console.log(Array.isArray(basket));

This prints:

3 apples
Priya 120
12
object object function
true
  • An array is an ordered list, read by position from 0.
  • An object holds named properties, read with a dot.
  • A function is a block of code that can be called with arguments.

typeof reports 'object' for an array, so use Array.isArray to tell an array apart from a plain object. Dates, maps, sets and the elements of a web page (the DOM) are objects too.

const stops the variable from pointing at a different value. It does not stop the object it points at from changing:

const basket = ['apples', 'bread'];
basket.push('cheese');
console.log(basket);

This prints:

[ 'apples', 'bread', 'cheese' ]

To stop an object’s properties from changing, use Object.freeze, which Seal, Freeze, PreventExtension covers.

Steve Griffith covers this topic in his video Working with JavaScript Variables and Datatypes.