Js Closures | Table O Contents

Closures are properties of functions in Javascript that can be executed in scopes other than the scope the subject closure originated.

Note. The Flammarion Logo Badge in the page header above is an .svg image file set to the dimensions of 5% width, auto height, and zoom. Go ahead and test the zoom-out feature by hovering over the badge to engage the expansion of the image.

Incremental Closures

Hint. Only functions have closures …

The Code

// Set up a `function scope` named `handleLikePost` to increment the `likeCount` variable
function handleLikePost() {
  // Set the `likeCount` to `default zero`
  let likeCount = 0;
  // Note. Nested functions can be anonymous ie.) no name required
  // Return the nested function ... 
  // Disallow the outer `main` function further with a `return` statement

  return function addLike() {
    // The nested function `addLike` will increment the `likeCount` variable by `1`
    likeCount += 1;
    // Next, make the variable `likeCount` available outside both the nested function ...
    // As well as the `main` function using a `return` statement
    return likeCount;

// Execute the `main` function by calling the constant `like`
const like = handleLikePost();

// Return the appropriate repeated responses to the console

Last Subtitle

More to come …

Note. The above synopsis was derived from a video lecture written, produced and performed by …

Reed Barger: [1].

For additional reference on the topic of Ternary Operators see the Mozilla Developer Network: [2]

For a working Internet development platform for Javascript Closures see Scrimba: [3]

  1. Javascript: The Complete Developer, 2020

  2. Mozilla Developer Network

  3. Scrimba Javascript Developer Platform


Please support the co-workers who aggregate the Source Links for our projects.


Like what you see in this project? If so, then support the authors and machine-elves who aggregate the source links and pages for our projects via Patreon.