Variables & values

A place for your data

Lesson 2 of 20 · 6 min · Module 1: Values & first steps

Variables give a name to a value so you can use it again. Think of them as little labeled containers.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Create named values
  • Distinguish strings from numbers
  • Choose const or let deliberately

The idea

Create a variable with const when its value will stay the same. Use let when you want to change it later.

const language = "JavaScript";
let lessons = 1;
console.log(language);

01A name refers to a value

The equals sign assigns a value to a variable. A later console.log(name) reads that stored value. It does not print the word name unless you put name inside quotes.

const name = "Alex";
console.log(name);
Output
Alex

02Reassignment is a separate operation

Use let when the variable will later refer to a new value. const prevents reassignment of that binding; it does not make every object stored in it deeply immutable.

let lessons = 1;
lessons = lessons + 1;
console.log(lessons);
Output
2

Your turn

Create const name with the value "Alex" and let age with the number 21. Print both values.

What the checks look for

  • name holds the string Alex
  • age holds the number 21
  • Both values are printed
Try it in the editor →

Take this with you

Name data clearly and keep its type consistent with how you intend to use it.

Learn JavaScript Foundations for free

20 short lessons with real code and instant checks.

See the whole course →