JavaScript Fundamentals: var vs let, Closures, and Polyfills
viaLeetCode
Q&A covering:
- var vs let:
varis function-scoped and hoisted with an initial value ofundefined, and allows redeclaration in the same scope.letis block-scoped, hoisted into a temporal dead zone (cannot be used before declaration), and cannot be redeclared in the same scope. - Closures: a function retains access to variables from its enclosing lexical scope even after the outer function has returned -- commonly used for data privacy/encapsulation and factory functions.
- Polyfills: code that implements a feature not natively supported in the current environment/browser, letting you use modern APIs (e.g.
Array.prototype.includes,Promise) on older runtimes by providing an equivalent implementation.
asked …