


Unraveling JavaScript: A Deep Dive into Hoisting, Temporal Dead Zone, and Variable States
In below I have sweet and simple 2 lines of code. But I can guranty you that It will either confused a lot (as because you ignored the underline principle of JS) or comfort you.
But it has fully loaded knowledge concepts as below
- Hoisting
- Temporal dead Zone
- variable (undeclare, uninitialized, undefine) (Bonus)
My contradicting Statement
as like var, const and let also hoist their properties, but they are in different zone.
hoisting Def (Simple /layman version)
- we can access functions and variables before its actual declaration.
Now Its time to go deep drive into how Js compile and execute our 2 lines code
In JavaScript, the way the compiler and engine handle variable declarations and assignments can be nuanced, especially when dealing with let and var.
Let's break down the process from both the compiler and execution perspectives for the given code:
name = 'ashu'; let name;
at this point i am making clear that when we write the javascript code, 1st parser and compiler compiles our code then it goes into execution phase.
Compiler Perspective
First Line: name = 'ashu';
During the compilation phase,
the JavaScript engine parses the code and creates the necessary scopes.
The assignment name = 'ashu';
will be noted, but at this stage, the engine does not execute the code; it merely records the existence of an assignment to a variable named name.
If name has not been declared before, the compiler treats it as an assignment to a global variable (var name in the global scope) since var declarations are hoisted and accessible globally.
Second Line: let name;
When the compiler encounters the let name; declaration, it acknowledges that name should be block-scoped.
The compiler places name in the Temporal Dead Zone (TDZ) for the scope it belongs to,
meaning it acknowledges the existence of name but marks it as uninitialized.
The let declaration is not hoisted in the same way as var.
Instead, it creates a binding in the scope and initializes it only when the declaration is executed.
Execution Perspective
First Line: name = 'ashu';
When the JavaScript engine executes the assignment name = 'ashu';,
it checks for the existence of name in the current scope. Since name is declared with let but is in the TDZ (Temporal Dead Zone), any attempt to access it before the let declaration is initialized will result in a ReferenceError.
Hence, at this point, name is in the TDZ, and the assignment name = 'ashu'; results in a ReferenceError.
Second Line: let name;
This line initializes the name variable within the block scope.
After this point, name is no longer in the TDZ and can be accessed or assigned without error.
Now Bonus tip
difference Between undeclare vs undefined vs uninitialised;
undeclare :- variable is not declare yet.
undefined :- Variable declared but not initialized;
uninitialised :- variable defined but its value will came later part.
Ex:- const result = multiplyBy2(5);
untill the retun value of the funciton will assigned to result till that point it would be in un initialized zone.
intersting Fact:-
You know the temporal dead zone initially decorated for Const but lates point in time they adopted in **Let**
Reference:-
- https://frontendmasters.com/courses/deep-javascript-v3 + My analogy along with GitHub co-pilot
The above is the detailed content of Unraveling JavaScript: A Deep Dive into Hoisting, Temporal Dead Zone, and Variable States. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
