QuickDevGuide

Home › JavaScript

Functions, Scope & Closures — JavaScript

Functions are first-class citizens in JavaScript — they can be assigned to variables, passed as arguments, and returned from other functions. There are three ways to define them: function declarations (hoisted), function expressions (not hoisted), and arrow functions (=> concise, no own this). Scope Scope determines where variables are accessible. Global scope: accessible everywhere (minimize pollution). Function scope: variables inside a function stay inside. Block scope (let/const): variables inside {} stay in that block. Hoisting: var declarations and function declarations are moved to the top of their scope by the engine at compile time. Closures Closures are one of JavaScript's most powerful features. A closure is when an inner function retains access to variables from its outer…

Key Takeaways