Data Privacy Using Closures in JavaScript Last Updated : 28 Apr, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Let's first understand what closures are in Javascript. Closures: A function along with a reference to the outer environment together forms a closure. In other words, we can say that it's a combination of a function and its lexical scope bundled together which means the inner function has access to its outer lexical environment i.e. access to the variables of the parent function together forms a closure. Example: JavaScript <script> function x() { var a = "GeeksforGeeks"; function y() { console.log(a); } return y; } x()(); </script> Output: Before discussing data privacy, let's understand encapsulation and data hiding. Data hiding is like we have a variable and we want privacy over it so that no other functions or other parts of the code can have access over it is called Data hiding or data privacy. In other words, we are just encapsulating the data so that other parts of the code can't have access to it. Let's Understand with the help of an example: Here we have a counter variable that we can access from the outside of the increase function from in part of the code. JavaScript <script> var counter = 0; function increase() { counter++; console.log("Access of counter from inside " + "the function as it forms a closure", counter); } increase(); // No data Privacy console.log("Accessed from outside also no " + "data privacy", counter); </script> Output: So on the above part, we can see that data is not hidden or no data privacy. Now let's apply the concept of closures. We will wrap the increase function with a function. So that the increase function will have access over its lexical environment only. JavaScript <script> function count() { var counter = 0; function increase() { counter++; console.log("Access of counter from " + "inside the function as it forms" + " a closure", counter); } return increase; } count()(); // Data Privacy is there is // it will throw error console.log("Accessed from outside also no" + " data privacy", counter); </script> Output: Here now after wrapping the increase function under a function, we can maintain data privacy and we can not call counter outside the function. This is all due to closures. It played the main role by limiting the access of counter within the lexical environment of the increase function . Comment More infoAdvertise with us Next Article How closure works in JavaScript ? U user_u3so Follow Improve Article Tags : JavaScript Web Technologies javascript-oop JavaScript-Questions Similar Reads How closure works in JavaScript ? In this article, we will discuss about the closures working JavaScript. Let us first understand what exactly closures are and basic details which are associated with closures in JavaScript.A Closure is a combination of a function enclosed with references to its surrounding state (the lexical environ 2 min read Closure in JavaScript A closure is the combination of a function and its lexical environment, which includes variables from the outer (enclosing) function's scope. This allows the inner function to continue to access the variables from the outer function, even after the outer function has finished executing.Closures are 5 min read Closure in JavaScript A closure is the combination of a function and its lexical environment, which includes variables from the outer (enclosing) function's scope. This allows the inner function to continue to access the variables from the outer function, even after the outer function has finished executing.Closures are 5 min read Concept of JavaScript Closures Inside Loops JavaScript is a versatile language that is used extensively for creating interactive web applications. One of its unique features is the ability to create closures, which are functions that have access to variables in their outer scope. Closures can be a powerful tool in programming, but they can al 5 min read What is the practical use for a closure in JavaScript? In JavaScript, closures are defined as inner functions that have access to variables and parameters of outer function even after the outer function has returned. The below examples show the practical use of closures: Example: In this example, a variable mult is defined that is local to the function 2 min read How to use a closure to create a private counter in JavaScript ? A closure is a combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer functionâs scope from an inner function. Example: Here the inner forms closure with outer. The str variable 4 min read Like