Skip to main content

Scope of a function

1. Definition

A function's scope is the internal "visibility zone" created when a function is called.

All variables declared with var, let, const, or function inside the function are accessible only inside that function and its nested functions.

In other words:

  • variables "live" only inside the function where they are declared;
  • they cannot be seen from outside.

2. Example

javascript
function sayHi() { const name = 'Bohdan'; console.log('Hello,', name); } sayHi(); console.log(name); // ReferenceError: name is not defined

The variable name is available only inside the function sayHi(). It does not exist outside - because this is function scope.


3. A function creates its own "lexical environment"

Every function call creates a new environment (scope), where its own variables live.

javascript
function counter() { let count = 0; return () => ++count; } const c1 = counter(); const c2 = counter(); console.log(c1()); // 1 console.log(c1()); // 2 console.log(c2()); // 1

Here c1 and c2 have different scopes with different count variables.


4. Scopes are nested inside one another

JS uses lexical (static) scope - a function "remembers" where it was declared, not where it was called.

javascript
let a = 10; function outer() { let b = 20; function inner() { let c = 30; console.log(a, b, c); } inner(); } outer(); // 10 20 30
  • inner has access to a, b, c;
  • outer has access to a, b;
  • the global scope has access only to a.

This is the scope hierarchy (scope chain).


5. var scope vs let / const scope

DeclarationScope
varFunction scope (the whole function)
let, constBlock scope (inside { ... })

Example:

javascript
function test() { if (true) { var x = 1; let y = 2; } console.log(x); // 1 console.log(y); // ReferenceError } test();

6. Example of "leaking" without function scope

javascript
if (true) { var x = 10; } console.log(x); // 10 - the variable "leaked"

var has no block scope, only function scope. If you wrap the code in a function, it becomes safe:

javascript
function run() { var x = 10; } console.log(x); // ReferenceError

7. Global vs local scope

LevelWhere the variable is visibleExample
GlobalEverywhere in the codelet appName = "MyApp"
FunctionOnly inside the functionfunction foo() { let x = 1; }
BlockOnly inside {}if (...) { let y = 2; }

8. Scope and closures

Closures use function scope to "capture" variables from the outer scope:

javascript
function makeCounter() { let count = 0; return function() { return ++count; }; } const counter = makeCounter(); console.log(counter()); // 1 console.log(counter()); // 2

The function "remembers" the scope where it was created.


SUMMARY

PropertyDescription
What it isThe area where variables and functions are visible
When it is createdWhen a function is called
Where variables are visibleInside the function and nested functions
What is not visibleVariables outside the function
var's scope typeFunction
let/const's scope typeBlock
Related toClosures, lexical environment, scope chain

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.