Skip to main content

Functions and Scope

Functions are the primary building blocks of any JavaScript program. They let you wrap up a block of code, name it, and reuse it. Understanding scope — where variables are accessible — is equally important.

Function Declarations

A function declaration defines a named function that is hoisted to the top of its scope.

Ctrl+Enter
HTML
CSS
JS
Preview

Because declarations are hoisted, you can call them before they appear in your code:

Ctrl+Enter
HTML
CSS
JS
Preview

Function Expressions

A function expression assigns a function to a variable. It is not hoisted.

Ctrl+Enter
HTML
CSS
JS
Preview

Arrow Functions

Arrow functions provide a shorter syntax and do not have their own this binding.

Ctrl+Enter
HTML
CSS
JS
Preview

When to Use Each

StyleHoistedOwn thisBest For
DeclarationYesYesTop-level named functions
ExpressionNoYesCallbacks, conditionals
ArrowNoNoShort callbacks, array methods

Default Parameters

You can assign default values to function parameters.

Ctrl+Enter
HTML
CSS
JS
Preview

Rest Parameters

The rest operator ... collects remaining arguments into an array.

Ctrl+Enter
HTML
CSS
JS
Preview

Understanding Scope

Scope determines where variables are accessible. JavaScript has three types of scope.

Global Scope

Variables declared outside any function or block are globally accessible.

Ctrl+Enter
HTML
CSS
JS
Preview

Function Scope

Variables declared with var inside a function are only accessible within that function.

Ctrl+Enter
HTML
CSS
JS
Preview

Block Scope

Variables declared with let or const inside a block ({}) are only accessible within that block.

Ctrl+Enter
HTML
CSS
JS
Preview

This is why let and const are preferred over var — they give you more predictable scoping.

Closures

A closure is a function that remembers the variables from its outer scope even after the outer function has returned.

Ctrl+Enter
HTML
CSS
JS
Preview

Closures are used everywhere in JavaScript — event handlers, callbacks, module patterns, and React hooks all rely on them.

Higher-Order Functions

A higher-order function either takes a function as an argument or returns a function.

Ctrl+Enter
HTML
CSS
JS
Preview

Immediately Invoked Function Expressions (IIFE)

An IIFE runs immediately after it is defined. It creates a private scope.

Ctrl+Enter
HTML
CSS
JS
Preview

IIFEs were essential before ES modules existed. Today they are less common but still useful for one-time initialization.

Practical Exercise

Build a simple task manager using closures:

Ctrl+Enter
HTML
CSS
JS
Preview

Key Takeaways

  • Function declarations are hoisted; expressions and arrow functions are not.
  • Arrow functions do not have their own this — use them for callbacks and array methods.
  • let and const are block-scoped, giving you tighter control over variable visibility.
  • Closures let inner functions access outer variables after the outer function has returned.
  • Higher-order functions accept or return other functions, enabling powerful composition patterns.