Skip to content
FORGENO ADS GAMESUnlock all $8

JavaScript · Unit 1: JavaScript basics · Lesson 2 of 5

Variables: let and const

A variable is a name for a value.

Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.

The idea

A variable is a name for a value. const creates one that is never given a new value: const city = "Lisbon";

let creates one you can change later: let score = 0; and then score = 10; Use const by default, and let only when the value must change.

Names are case-sensitive and cannot contain spaces. Most JavaScript uses camelCase, like totalPrice. You may see var in older code; let and const replaced it because they are safer.

Worked example

JavaScript

const name = "Ana";
let points = 5;
points = points + 3;
console.log(name, "has", points, "points");

It prints

Ana has 8 points

Key terms

Variable
A name for a value, created with let or const.
let and const
const for values that never get reassigned, let for values that change. Avoid the older var.

Practise it in Code Academy

It has 4 exercises (multiple choice, predict the output, fix the broken code and write code from a prompt) and takes about 7 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.

Every lesson is free: no ads, no lives and no paywall.