nested function scope

PHOTO EMBED

Mon Apr 29 2024 08:36:24 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #functions #nesting #scope

function firstFunction() {
  // parent function
  const x = 100; // parent variable / cant be used inside the child function

  function secondFunction() {
    const y = 60; // child variable / cannot be called in the parent function
    return y + x; // return calculation to the child function
  }

  return secondFunction(); // evoke the second function by returning it to the parent function
}

console.log(firstFunction()); // call the parent function
content_copyCOPY