JavaScript · Course navigation

JavaScript Variables

Use const and let for values.

On this page

The concept

Use const when you will not reassign a binding. Use let when reassignment is needed. Prefer clear names and avoid var in new code.

Code example

JavaScript
const name = "Ada";
let score = 82;
score += 9;
console.log(name, score);
Output
Ada 91

Follow the code

Open the walkthrough to inspect the example one step at a time.

Show guided walkthrough
Guided walkthroughReady
1const name = "Ada";
2let score = 82;
3score += 9;
4console.log(name, score);

Follow the example

Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.

View the complete dry-run table
Prepared trace of the published program. Some rows group repeated iterations.
Step / lineWhat happensState after stepOutput so far
Bind name to Ada.name = "Ada"
—
Initialize score to 82.score = 82
—
Add 9 and assign the result back to score.score = 91
—
Log both values.score = 91
Ada 91

Behind the scenes

How JavaScript processes this code

A JavaScript engine parses and runs the source. A function body runs when the function is called. console.log writes to the console.