Where Names Live in Blocks and Functions

In the last few lessons, your functions grew their own variables, like the age parameter of ticketPrice(). Each of those names worked fine inside its function. But try to use one somewhere else in the file, and JavaScript may tell you that the name doesn’t exist at all.

Every name you create lives in a particular part of your program, and only lines in that part can use it. In this lesson, you’ll learn the rule that decides where a name is visible. Then you’ll use it to predict which variable a line refers to and to place each declaration where the lines that need it can see it.

Keep working in your scratch/ folder. Several programs here stop with an error on purpose, so predict each output first. An error you saw coming shows that your picture of the code is right.

A Block Keeps Its Names Inside

At the arcade, a player who scores over 100 points earns a gold badge. Create scratch/badge.js, which declares the badge inside an if block and shows it there:

Language: JavaScript Filename: scratch/badge.js
const score = 120;

if (score > 100) {
  const badge = "Gold";
  console.log(`You earned a ${badge} badge!`);
}

The score is over 100, so the block runs:

Language: Shell
$ node scratch/badge.js
You earned a Gold badge!

A block is the code between a pair of curly braces, and a variable declared with let or const inside a block belongs to that block. Picture each block as a box drawn around its names. Lines inside the box can use a name once its declaration has run, but reading it on an earlier line stops the program. Once the block ends, the name is gone.

The part of a program where a name is visible is called its scope, so the scope of badge is the if block. The block also uses score, which was declared outside it. A line inside a block can see out, but a line outside can’t see in.

Now use that rule. Add a line after the block that shows the badge again, and predict what it does before you run the file:

Language: JavaScript Filename: scratch/badge.js
const score = 120;

if (score > 100) {
  const badge = "Gold";
  console.log(`You earned a ${badge} badge!`);
}

console.log(`Badge: ${badge}`);

The new line sits outside the box, so it can’t see badge:

Language: Shell
$ node scratch/badge.js
You earned a Gold badge!
ReferenceError: badge is not defined

Above the error, Node also prints the file’s path with a line number and the failing line with a caret (^) under the problem. You can skip the lines below it that start with at.

A ReferenceError means that a line used a name JavaScript couldn’t find, and badge is not defined tells you which name. The path line ends in :8, pointing to the line that asked for it.

It’s the braces that make the box, not the if. A pair of braces on its own, called a bare block, keeps its names inside too. You’ll rarely need one.

A Function Body Is a Box Too

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.