JS Built-ins
Closuresbeginner

Closures β€” Closure over const

πŸ“– Usage Example
const PI = 3.14159
const circleArea = r => PI * r * r
circleArea(5)   // β†’ 78.53975
circleArea(10)  // β†’ 314.159

Arrow functions close over const variables in the surrounding scope, making constants immediately available without passing them as arguments.

Capturing const Variables

A closure can capture variables declared with const, creating a lasting reference.

Challenge: Declare a const variable and create a function that returns it. Call the function and verify it returns the correct value.

Example:

const x = 10
const getX = () => x
getX()  // 10
πŸ’‘ Hints (3)
  • β€’ const creates a read-only binding
  • β€’ Closures can still access const variables
  • β€’ The closure captures the binding, not the value