closure function in javascript example

Understanding JavaScript: A Comprehensive Guide to Closure Function Examples

By Olivia Lewis
AD

In the dynamic world of JavaScript, function expressions are a powerful tool, allowing us to create and manipulate functions on the fly. One unique type of function expression is the **closure function**, which encapsulates its scope, maintaining access to variables from the outer scope even after the outer function has finished execution. Let's dive into the fascinating realm of closure functions in JavaScript, complete with practical examples.

Understanding Closure Functions

Closures are typically created every time a function is defined, at function creation time. A closure gives you access to an outer function's scope from an inner function. This can be particularly useful when you want to create private variables or data hiding.

To illustrate, consider the following example:

AD
What are Closures in JavaScript?
What are Closures in JavaScript?
Closures in JS
Closures in JS
the text reads, understanding closures in jawscript - javescript in plain english
the text reads, understanding closures in jawscript - javescript in plain english

Encapsulating Scope

Here's a simple closure example that demonstrates encapsulating scope:

function outer() {
    let counter = 0;
    function inner() {
      counter++;
      return counter;
}    return inner;
}

let outerFn = outer();
console.log(outerFn()); // Outputs 1
console.log(outerFn()); // Outputs 2

Back to Basics - JavaScript Closures
Back to Basics - JavaScript Closures
How JavaScript Closure Can Make You Sick?
How JavaScript Closure Can Make You Sick?
Javascript Closure tutorial ( Closures Explained )
Javascript Closure tutorial ( Closures Explained )

Creating Counter Factory

Now, let's explore creating a counter factory using closure:

function counterFactory(startingValue) {
  let counter = startingValue;
  function incrementAndReturn(){
    counter++;
    return counter;
}    return incrementAndReturn;
}

let counter1 = counterFactory(5);
console.log(counter1()); // Outputs 6
console.log(counter1()); // Outputs 7
let counter2 = counterFactory(10);
console.log(counter2()); // Outputs 11

AD

Closures and Memory Efficiency

Closures provide an excellent means of data encapsulation and can lead to more memory-efficient code by avoiding global variables and reducing the scope of variables.

How do JavaScript closures work?
How do JavaScript closures work?
Here’s What You Have To Know About JavaScript Closuers
Here’s What You Have To Know About JavaScript Closuers
JavaScript Closures 101: What is a closure?
JavaScript Closures 101: What is a closure?
Learn About JavaScript Closures: A Beginner's Guide
Learn About JavaScript Closures: A Beginner's Guide
JavaScript Return Values: Get the Most Out of Your Functions 🚀
JavaScript Return Values: Get the Most Out of Your Functions 🚀
Understanding closures in JavaScript
Understanding closures in JavaScript

For instance, if we compare the previous counter factory example with a global counter approach, we can see the difference:

Global Counter vs. Closure Counter

Global Counter:

AD

let globalCounter = 0;
function incrementAndReturn() {
globalCounter++;
return globalCounter;
}

a piece of paper with some type of text on it and an image of a backpack
a piece of paper with some type of text on it and an image of a backpack

Closure Counter:

function counterFactory(startingValue) {
  let counter = startingValue;
  function incrementAndReturn() {
    counter++;
    return counter;
}    return incrementAndReturn;
}

AD

As each closure maintains its own counter, memory usage is more efficient compared to the global counter approach.

In conclusion, closure functions in JavaScript enable us to harness the power of private variables and data encapsulation, leading to more efficient and organized code. By understanding and utilizing closures, you can elevate your JavaScript skills and craft more robust and maintainable code. Keep practicing and exploring to fully grasp the magic of closures!

AD