Exerpad
Lesson · Chapter: Variables+10 XP for reading

What are Variables?

A variable is like a labeled box that stores a value. You give the box a name, and you can put something inside it.

Creating Variables

Use let and = to store a value in a variable:

javascript
let name = "Alex";
let age = 10;
Output
Press Run to see the output.

If a value never changes, use const instead:

javascript
const birthYear = 2016;
Output
Press Run to see the output.

Types of Values

Strings — text inside quotes:

javascript
let color = "blue";
let greeting = 'Hello!';
Output
Press Run to see the output.

Numbers — whole numbers and decimals:

javascript
let score = 100;
let temperature = 21.5;
Output
Press Run to see the output.

Booleans — true or false (always lowercase in JavaScript!):

javascript
let isHappy = true;
let isRaining = false;
Output
Press Run to see the output.

Naming Variables

JavaScript names are usually written in camelCase: the first word is lowercase, and every next word starts with a capital letter, like myName or highScore. Names can't contain spaces or start with a number.

Using Variables

You can use variables in console.log():

javascript
let name = "Alex";
console.log(name);
Output
Press Run to see the output.

Output:

Alex

You can log variables with text:

javascript
let name = "Alex";
console.log("Hello,", name);
Output
Press Run to see the output.

Output:

Hello, Alex

Changing Variables

You can change what's inside a variable made with let:

javascript
let score = 0;
console.log(score);
score = 100;
console.log(score);
Output
Press Run to see the output.

Output:

0
100

The old value is replaced by the new one. Notice you only write let the first time — after that, just use the name.