JavaScript Closure
A Powerful Feature
An inner function that has access to
• its own variables,
• the outer enclosing function’s variables,
• the global variables.
This holds even when outer function has returned.
function favouriteBook(title, author) {
const intro = 'My favourite book is ';
return function book() {
return intro + title + ' by ' + author;
};
};
const favourite = favouriteBook('Eloquent JavaScript', 'Marijn Haverbeke');
console.log(favourite());
25/33