Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

What is the difference between global, function, and block scope?

Quick answer

Scope is the part of a program where a variable name can be accessed. Global scope is the surrounding top-level scope; function scope confines a name to its function; block scope confines it to a block such as an if statement. var is scoped to its containing function or top-level scope, not an ordinary nested block. let and const are limited to their block. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What does scope mean, and how do global, function, and block scope differ?

Geez

Scope is the region of code where a name can be used. Think of it as a boundary: a declaration outside a function or block is at the surrounding top level; a declaration inside a function may be limited to that function; and a declaration inside a smaller brace-delimited block may be limited to that block.

For the declarations in your question, the key distinction is that var is scoped to its containing function or top-level scope, not to an ordinary nested block. let and const are limited to their block. [S1] [S2]

One qualification: “global” does not mean every file or module can automatically access a name. The cited description of var includes module scope, so a declaration at a module’s top level belongs to that module’s scope. [S1]

Follow-up question

Can you show what happens when I declare each one inside an if block?

Geez

Here is a small example. Assume the condition is true, so the declarations run:

if (true) {
  var count = 1;
  let label = "inside";
  const fixed = "also inside";
}

console.log(count); // 1
console.log(label); // ReferenceError
console.log(fixed); // ReferenceError

count can be referenced after the if block because var does not become block-scoped just because it appears between those braces. label and fixed are block-scoped, so they are unavailable outside that block. [S1] [S2]

The comments describe what happens as each line runs in order. If you run the whole snippet, execution stops at the first error; test the later references separately if you want to see each result.

Follow-up question

How does function scope change that? I’m not sure where the braces matter.

Geez

A function’s braces mark a function boundary. A var declared inside the function is confined to that function, even if its declaration sits inside an if block within the function. The let and const declarations in the if block remain confined to that smaller block.

function showValues() {
  if (true) {
    var total = 3;
    let note = "temporary";
  }

  console.log(total); // 3
  console.log(note);  // ReferenceError
}

showValues();
console.log(total); // ReferenceError

Inside showValues, total is available after the if because it uses var. Outside the function, it is unavailable because its containing function sets its scope. note is limited to the if block. These examples apply the function-scope rule for var and block-scope rule for let and const. [S1] [S2]

Follow-up question

Can you give me a quick comparison and a way to check my own code?

Geez

Use this comparison as a starting point:

  • Global or module level: A declaration outside a function is in the surrounding top-level scope. In module code, do not assume that means it is available in unrelated modules.
  • Function scope: var is available within its containing function, not just the nearest ordinary block. [S1]
  • Block scope: let and const are available only within the block where they are declared. [S2]

To check code, mark the braces for each function and each if, loop, or other block. Find the declaration, then ask: is the reference still within its containing function or top-level scope for var, or inside the specific block for let and const? If not, treat it as out of scope. A reference outside its scope produces an error when evaluated. This comparison focuses on scope, not every JavaScript rule that affects when declarations can be used.

A practical checklist

  • Find the declaration keyword: var, let, or const.

  • Mark the containing function’s opening and closing braces.

  • Mark smaller block braces, such as those around an if statement.

  • For var, check whether a reference remains within its containing function or top-level scope. [S1]

  • For let and const, check whether a reference remains inside the block where each was declared. [S2]

  • Test questionable references separately so one error does not stop the rest of an example.

A few more useful answers

Does `var` become block-scoped when it is inside an `if` statement?

No. var does not acquire ordinary block scope; it is scoped to its containing function or the relevant top-level scope. [S1]

Are `let` and `const` scoped to the whole function?

Not necessarily. They are limited to the block where they are declared, which can be a smaller block inside a function. [S2]

Does a top-level declaration always work across separate JavaScript modules?

These scope rules do not establish that a module-level name is automatically available in other modules. The description of var includes module scope. [S1]

Sources

References used for this guide.

  1. [S1] var — JavaScript | MDN
    developer.mozilla.org · Checked
  2. [S2] Block statement — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work