×

javascript · level 3 · unit 1 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Every variable lives in a , and only that scope can see it.

A variable made with let or const lives inside the block where you made it: the nearest pair of curly brackets around it. That region is its scope. Code inside the block can use the name. Code outside cannot, and gets a ReferenceError.

Names made outside every block live in the , so every part of the program can see them. Blocks nest like boxes inside boxes: inner code can look outwards, never inwards.

If an inner block makes a variable with a name already used outside, that is . Inside the block the new one wins. Outside, the old one is untouched.

const shop = "Corner Café";
function openUp() {
  const greeting = `Welcome to ${shop}`;
  console.log(greeting);
}
openUp();
console.log(typeof greeting);

typeof is the one safe way to ask about a name that does not exist here: it gives undefined instead of an error.