All lessons

Variables

6 min · JavaScript

Store values in named boxes called variables, then reuse them.

JavaScript · Variables0/5 cleared

Enemy 1 of 5

This code stores the text Ada in a variable called name using let, then prints it with console.log. Press Run to see it.

Why this works

A variable is a name that holds a value, like a labeled container you can refer back to later. Instead of retyping the same value everywhere it's needed, you store it once under a name and reuse the name, which becomes essential once a program is juggling more than a couple of values.

You make one with let: the keyword let, then the name, an equals sign, and the value. It helps to read = as "gets set to" rather than the math meaning of "equals": here it's an action (store this value under this name), not a statement that both sides are permanently identical.

Once a value is stored, you can use the name anywhere instead of repeating the value, and, since it's declared with let, you can also assign a new value to it later on and the variable will simply hold whatever was assigned most recently.

You
Cold-Start Frostling
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.