Scopes Cheatsheet
@new_javascript
1. Global Scope
Variables declared with var or let outside any
function or block are in the global scope,
meaning they can be accessed anywhere in
the code.
@new_javascript
2. Function Scope
Variables declared with var and let inside a
function are confined to that function's scope.
They cannot be accessed outside the function.
@new_javascript
3. Block Scope
Variables declared with let inside a block (e.g.,
{}) are confined to that block. var is not block-
scoped and can be accessed outside the block.
@new_javascript
4. Hoisting
Variables declared with var are hoisted to the
top of their scope, meaning they can be used
before their declaration but will have the value
undefined.
let is also hoisted but is not initialized, so
accessing it before the declaration results in a
ReferenceError.
@new_javascript
5. Redeclaration
var allows redeclaration within the same scope,
while let does not. Redeclaring a variable with
let in the same scope results in an error.
@new_javascript
6. Shadowing
A variable declared with let inside a block can
shadow a global or function-scoped variable
with the same name. The outer variable is
inaccessible inside the block.
@new_javascript
Are you looking for Front-end Developer
Job?
If yes, Check the link in bio to get
Interview Kit and be prepared for your
next Interview
@new_javascript