Scope, Closures and this

Block and function scope, hoisting, closures in practice, and how this is decided in methods, callbacks, arrow functions and classes.

Est. 40 min Updated Suggest a fix
// Part 01 — Where names live

Scope Controls Which Code Can See a Variable

Scope is the set of places where a name can be accessed. JavaScript looks for a name in the current scope first, then walks outward through enclosing scopes. This lexical lookup follows where functions and blocks are written, not which function happened to call them. Keeping variables in the narrowest useful scope reduces accidental coupling.

javascript
const course = 'JavaScript'

function describeCourse() {
  const level = 'Foundations'
  if (level) {
    const topic = 'scope'
    console.log(course, level, topic)
  }
  // topic is not visible here
}

describeCourse()
// level is not visible here either

let and const are block-scoped. A block is commonly the code inside braces in a function, conditional, loop, or standalone block. const prevents reassignment of the binding; it does not freeze an object stored in that binding. Prefer const by default, then use let when reassignment is part of the design.

🎯 Pro Tip
If a value is only used inside one function, declare it there. Smaller scope makes it easier to understand state and prevents unrelated code from silently depending on it.
// Part 02 — Before execution

Hoisting Does Not Mean Every Name Is Ready

JavaScript processes declarations before running a scope, but different declarations behave differently. Function declarations can be called before their written position. A var binding is available throughout its function scope and starts as undefined. let and const are also registered for their block, but cannot be read before their declaration is evaluated; that interval is the temporal dead zone.

javascript
console.log(readLabel())

function readLabel() {
  return 'Ready'
}

console.log(status) // undefined
var status = 'loaded'

// A let or const read before its declaration throws a ReferenceError.
// Keep declarations before their first use instead of relying on hoisting.
Practical declaration behaviour
DeclarationScopeBefore declaration runs
function declarationEnclosing scopeCallable in that scope
varFunction (or global) scopeBinding exists with value undefined
letBlock scopeIn temporal dead zone; reading throws
constBlock scopeIn temporal dead zone; reading throws

Avoid using var in new code: its function scope and redeclaration rules can make loop and conditional behavior surprising. Understanding it still helps when reading older scripts. Modern code is clearer when a binding is declared close to its first use.

// Part 03 — Functions remember

A Closure Keeps Access to Its Lexical Environment

A closure is a function together with access to the variables in the lexical environment where it was created. When an outer function returns an inner function, the inner function can still use those variables. Each call to the outer function creates a separate environment, which makes closures useful for independent state.

javascript
function createCounter(startAt = 0) {
  let count = startAt

  return function increment() {
    count += 1
    return count
  }
}

const firstCounter = createCounter()
const secondCounter = createCounter(10)
console.log(firstCounter()) // 1
console.log(firstCounter()) // 2
console.log(secondCounter()) // 11

The count variable cannot be accessed directly outside the returned function, but the closure can read and update it. This is encapsulation, not encryption: the value is still part of the running program. Closures also power event handlers, timers, and callbacks that need the values surrounding their creation.

📌 Real World Example
A delivery-fee calculator can close over a chosen service zone and return a function that prices many orders in that zone. Keep stable configuration in the closure and pass each changing order as an argument.
// Part 04 — A function's receiver

For Ordinary Functions, this Comes from the Call Site

The value of this is not determined by the function's name or where it is declared. For an ordinary function, inspect how it is called. Calling a method as object.method() gives that call the object as its receiver. Saving the method and calling it alone loses that receiver. In strict mode, a plain function call has undefined for this; do not depend on global-object behavior.

javascript
const learner = {
  name: 'Anika',
  introduce() {
    return 'Hi, I am ' + this.name
  },
}

console.log(learner.introduce())
const introduce = learner.introduce
console.log(introduce()) // this is not learner

The second call does not preserve the object relationship. If an operation needs a specific receiver, keep calling it as a method or create a bound function. This rule matters in callbacks because passing learner.introducepasses the function value, not a promise that it will later be called through learner.

Call formHow to reason about this
object.method()The object before the dot is the receiver
method() as a plain callNo receiver is supplied; strict mode gives undefined
method.call(value)The first argument supplies the receiver
boundMethod()The receiver is fixed when bind() creates the function
arrow()Uses this from its surrounding lexical scope
// Part 05 — Choose a binding

Use call, apply or bind When You Need a Receiver

call invokes a function immediately with a chosen receiver and separate arguments. applydoes the same with arguments in an array-like list. bind returns a new function with its receiver fixed; it does not run the function at bind time. These tools are useful for legacy APIs and callbacks, though a small wrapper function is often easiest to read.

javascript
function formatBalance(currency) {
  return currency + ' ' + this.balance.toFixed(2)
}

const wallet = { balance: 1250 }
console.log(formatBalance.call(wallet, '₹'))
console.log(formatBalance.apply(wallet, ['₹']))

const showWalletBalance = formatBalance.bind(wallet, '₹')
console.log(showWalletBalance())

Arrow functions are different: they do not have their own this, and call, apply, or bind cannot replace their lexical value. Use an arrow when you want a callback to keep the surrounding receiver. Use method syntax for object behavior that should receive the object at call time.

javascript
const dashboard = {
  name: 'Learner dashboard',
  scheduleGreeting() {
    setTimeout(() => {
      console.log('Welcome to ' + this.name)
    }, 0)
  },
}

dashboard.scheduleGreeting()
💡 Note
A class method is still an ordinary function with a receiver. Passing an unbound instance method as a callback can lose its instance. Bind it deliberately or use a wrapper callback.
// Part 06 — Debugging mental model

Trace the Binding and the Call Separately

When a value is unexpectedly undefined, ask two separate questions: which declaration supplies the name, and where was the function called? For a closure, follow the lexical scopes outward. For this, inspect the call expression rather than the function body. Breakpoints and small logging statements can show both without changing the design.

javascript
function makeGreeter(greeting) {
  return function greet(name) {
    return greeting + ', ' + name
  }
}

const sayHello = makeGreeter('Hello')
const sayKiaOra = makeGreeter('Kia ora')
console.log(sayHello('Ravi'))
console.log(sayKiaOra('Ravi'))

Each returned function refers to its own greeting. If a callback behaves differently from a direct method call, inspect whether the callback is invoked with the original receiver. These are separate mechanisms: lexical scope resolves names; the call site supplies this for ordinary functions.

  • Find the declaration that a name resolves to, starting from the innermost scope.
  • Check whether a let or const is being read before its declaration executes.
  • For ordinary functions, inspect the exact expression that calls the function.
  • For an arrow, look outward to the surrounding this rather than searching for a receiver at the call site.
// Part 07 — Pitfalls

Common Mistakes

Reading a let or const before its declaration

Why: The binding exists but remains in the temporal dead zone until its declaration runs.

Fix: Move initialization before use and keep declarations near their first use.

Expecting a returned function to lose its outer variables

Why: Closures retain access to their lexical environment after the outer call returns.

Fix: Use closures intentionally for private state and avoid capturing unnecessary long-lived objects.

Passing an object method directly as a callback

Why: The callback is invoked as a plain function, so its original receiver is not automatically preserved.

Fix: Pass a wrapper such as () => object.method(), or bind the method when creating the callback.

Using an arrow as a method that needs its own receiver

Why: An arrow inherits this from where it was created and cannot receive a replacement this.

Fix: Use method syntax for receiver-based object behavior; use arrows for lexical callbacks.

// Part 08 — Practice

Build a Small Configurable Study Helper

Practice

Closures and this in everyday code

  1. Write createCounter(startAt) with next(), current(), and reset(value) functions that share private count state.
  2. Create a formatter factory that closes over a currency symbol and formats several rupee amounts.
  3. Create an object with a method that returns a formatted message using this, then save and call the method separately to observe the difference.
  4. Fix the detached call with bind and with a wrapper arrow; explain how the two solutions differ.
  5. Write one ordinary callback and one arrow callback inside an object method. Predict and then log each this value.

Hint: Use a fresh object and small console.log checks. Keep scope lookup separate from reasoning about the call-site receiver.

🎯 Key Takeaways

  • ✓Scope determines where a variable name can be used; let and const are block-scoped.
  • ✓Function declarations, var, and let/const have different hoisting behavior; prefer declarations before use.
  • ✓A closure retains access to its lexical environment and can keep independent private state.
  • ✓For ordinary functions, this depends on how the function is called, not simply where it was written.
  • ✓call and apply invoke with a chosen receiver; bind creates a function with a fixed receiver.
  • ✓Arrow functions inherit this lexically and are useful for callbacks that need the surrounding receiver.

Next

What comes next

Transform collections with the array methods you will use in everyday applications.

Lesson 04 · Array Methods in Depth — map, filter, reduce, sort →

HELP US IMPROVE

How was this lesson?

Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.

Choose your feedback

Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.

Share

Discussion

0

Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.

Continue with GitHub
Loading...