Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
All about Variables  ›  Lesson

var, let and const

Variable Declarations 12 minconst declares a name that cannot be reassigned, let declares one that can, and var is the older keyword that modern code avoids.

var, let and const

In short: default to const — a name that can never be reassigned once given a value. Use let when the name must point at something else later. Leave var, the 1995 keyword, to old code: it ignores block braces and allows quiet redeclaration, which hides mistakes. The one surprise: const fixes the name, not the contents of an object or array behind it — you can still change what's inside.

Three boxes: const title has a welded lid (name can't move), let plays has a hinged lid (reassign anytime), var old has no lid at all (leaks past braces) — with a note that const song = { plays: 0 } still allows song.plays = 1 but not reassigning song itself

Track a song's play count

A playlist page needs a fixed title and a play count that goes up every time the song finishes. Pick const for the name that never moves, let for the one that does:

javascript

The count updates fine — let allows reassignment. The last line throws TypeError: Assignment to constant variable. because title was declared const. Note the count already printed 1 before the error: JavaScript runs each line until it hits the one that fails, it does not undo what already ran.

Grow a liked-songs list without touching its name

const blocks moving the name to a different value — it says nothing about what lives inside an object or array:

javascript

This runs cleanly and prints 1. song still points at the exact same object; only a property inside it changed. song = { title: "Red Dawn" } would fail, because that line tries to point the name at a brand new object — the one thing const forbids.

Declaring the playlist's variablesStep 1 of 4
  1. The song's title

    "Blue Hour" never changes while the page runs. Never reassigned → const title = "Blue Hour";

A const with no starting value

const must receive its value on the line it is declared — there is no "decide later":

javascript

This throws SyntaxError: Missing initializer in const declaration before a single other line of the program runs. let next; is legal and starts as undefined; const total; is not legal at all.

Quick practice

quick drill

What happens when const plays = 0; plays = 1; runs?

quick drill

What does const song = { plays: 0 }; song.plays = 5; console.log(song.plays); log?

quick drill

What happens with the line const total;?

quick drill

Which keyword fits a counter that goes up by one after each song?

quick drill

Which keyword lets you declare the same name twice in the same scope without any error?

🎯 Your turn

A playlist page needs: the playlist's fixed name, a volume the listener can change, and an array of liked songs that grows as they tap a heart. Declare each one and explain your choice of keyword.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainChoose the keyword

A playlist page needs: the playlist's fixed name, a volume the listener can change, and an array of liked songs that grows as they tap a heart. Declare each one and explain your choice of keyword.