How can closures be used to create private variables in JavaScript?

  • By declaring variables with the private keyword inside a function.
  • By defining variables in the global scope.
  • By using the const keyword to declare variables.
  • By declaring variables inside a function that is not accessible from outside the function.
Closures can be used to create private variables in JavaScript by declaring variables inside a function. These variables are not accessible from outside the function, effectively creating encapsulation and data hiding.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *