How can you create a private variable in JavaScript?

How can you create a private variable in JavaScript?

a) Using closures
b) Using var outside a function
c) Using this keyword
d) Using let outside a function

Answer:

a) Using closures

Explanation:

Private variables in JavaScript can be created using closures. A closure is an inner function that has access to the outer function’s variables, even after the outer function has returned. This allows private variables to be encapsulated within a function.

Reference:

JavaScript MCQ (Multiple-Choice Questions)

Leave a Comment

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

Scroll to Top