In which scenario might a closure be particularly useful?

  • When you want to declare a global variable.
  • When you want to protect variables from being modified.
  • When you need to maintain access to local variables after the parent function has finished.
  • When you want to create a private method in an object.
Closures in JavaScript are particularly useful when you want to create private variables or methods in an object. They allow you to maintain access to local variables even after the parent function has completed its execution. Closures help with data encapsulation and information hiding.
Add your answer
Loading...

Leave a comment

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