NamsteJS - 2
NamsteJS - 2
- **Definition of Hoisting:**
- When a variable is declared, its declaration is hoisted to the top, but its value
remains uninitialized until the code execution reaches the line where the
assignment occurs.
- For example, if a variable `x` is declared but not initialized, accessing it before
initialization results in `undefined`.
- Attempting to access a variable that has not been declared will throw a
ReferenceError.
- Function declarations are fully hoisted, meaning you can call them before the point
at which they are defined in the code.
- Arrow functions and function expressions, however, are not hoisted in the same
way as regular function declarations.
- If an arrow function is assigned to a variable, that variable's declaration is hoisted,
but the function itself is not defined until the assignment is reached.
- Each time a function is invoked, a new execution context is created. This context
contains information about the function's local scope and any variables defined
within.
- Once a function completes execution, its context is removed from the call stack,
and JavaScript reverts to the previous context.
- **Call Stack Behavior:**
- The call stack keeps track of function invocations. When a function is called, its
context is pushed onto the stack.
- After the function execution finishes, its context is popped off the stack. This helps
manage multiple function calls correctly and maintain the order of execution.
- Many developers mistakenly believe that hoisting applies uniformly to all kinds of
functions. However, arrow functions behave like variables in terms of hoisting and
cannot be invoked before their assignment.
- This is crucial for avoiding runtime errors and ensuring code behaves as expected.
- **Practical Implications:**
- The variable environment refers to the context in which variables are declared and
defined, influencing their accessibility and lifespan.
- Each function creates a new variable environment, storing local variables that are
not accessible outside the function scope.
- Global variables exist outside any function and can be accessed throughout the
code, while local variables are confined to their respective functions.
**Execution Context**
- Invoking a function can pass arguments, which are treated as local variables within
that function's execution context.
- The scope of a variable determines its visibility: local variables are only accessible
within their function, while global variables can be accessed anywhere.
- Understanding the scope is essential for debugging and structuring code to avoid
naming conflicts and ensure proper variable access.
3. SHORTEST JS Program 🔥window & this keyword | Namaste JavaScript Ep. 5
Key Insights
- Summary
o The video explains the difference between “undefined” and “not defined” in
JavaScript, highlighting how variables are allocated memory.
- Highlights
o Memory Allocation: JavaScript allocates memory for variables before
executing the code, initially setting them to “undefined.”
o Difference Between Undefined and Not Defined: “Undefined” is a memory
placeholder, while “not defined” indicates that no memory has been
allocated.
o Types in JavaScript: JavaScript is a loosely typed language, allowing variables
to hold different data types at different times.
o Best Practices: Avoid assigning “undefined” to variables as it can lead to
inconsistencies in the code.
o Flexibility of JavaScript: The language is appreciated for its flexibility in
handling different data types without strict constraints.
- Keywords
o Undefined
o Not Defined
o JavaScript
o Memory Allocation
o Loosely Typed Language
5. The Scope Chain, 🔥Scope & Lexical Environment | Namaste JavaScript Ep.
7
- Lexical Environment:
o A structure that holds variable bindings (i.e., variables and functions) within
a defined scope.
o Important for understanding how variables are accessed in different
contexts.
- Scope:
o Refers to the accessibility of variables in different parts of the code.
o Two main types: Global Scope and Local Scope.
- Execution Context:
o Representation of the environment in which a piece of code is executed.
o Comprised of the Lexical Environment and the value of this.
- Practical Examples
o Functions have their own Lexical Environment, which enables the creation of
closures.
o When a variable is not found in the local context, JavaScript searches in the
global context.
- Important Functions
o Function Invocation: How functions are executed. The context influences
variable accessibility.
o Closures: A function that retains access to its outer lexical environment even
when executed outside its original scope.
- Visual Representation
o Diagram of Lexical Environment: Represents how variable bindings are
stored and accessed at each level of scope.
- Reflection
o Consider how Lexical Environment impacts variable management in larger
applications.
o Explore the relationship between global and local scopes in complex
JavaScript applications.
6. let & const in JS 🔥Temporal Dead Zone | | Namaste JavaScript Ep. 8
- Hoisting Behavior
let and const are hoisted: During the memory allocation phase, these variables
are hoisted but not initialized.
Temporal Dead Zone (TDZ): The period between hoisting and initialization
where accessing these variables results in a Reference Error.
var vs. let/const:
o var: Hoisted and initialized with undefined; accessible before declaration.
o let/const: Hoisted but not initialized; accessing before declaration throws an
error.
- Scope and Accessibility
Block Scope: let and const are block-scoped, meaning they are only accessible
within the block they are defined.
Global Object:
o var variables become properties of the global window object in browsers.
o let and const do not attach to the window object.Scribd
- Redeclaration and Reassignment
let:
o Cannot be redeclared in the same scope.
o Can be reassigned.
const:
o Cannot be redeclared or reassigned.
o Must be initialized at the time of declaration.Scribd
- Errors Explained
ReferenceError: Accessing a variable in its TDZ.
SyntaxError: Incorrect syntax, such as missing initializers in const declarations.
TypeError: Reassigning a const variable.
- Best Practices
Prefer const: Use const by default for variables that shouldn't change.
Use let when necessary: Opt for let when variable reassignment is needed.
Avoid var: Due to its function-scoped nature and hoisting behavior, it's
recommended to avoid using var.
Declare variables at the top: This practice minimizes the TDZ and potential
errors.