JavaScript Variables: Var, Let, Const, and TDZ Explained

Day 2/100 of JavaScript 🚀 Today’s Topic: "let", "const", "var", hoisting and TDZ. "var", "let", and "const" are used to declare variables, but they differ in scope and initialization behavior - "var" is function-scoped and during the creation phase it gets initialized with "undefined", so it can be accessed before assignment. - "let" and "const" are block-scoped and are registered in memory during creation, but not initialized immediately. This leads to TDZ (Temporal Dead Zone) a phase where the variable exists in memory but remains uninitialized and cannot be accessed. Accessing "let" or "const" variables before initialization results in a ReferenceError. - "const" must be initialized at declaration and cannot be reassigned. - "let" allows reassignment but not redeclaration in the same scope. These differences make "let" and "const" more predictable and safer compared to "var". #Day2 #JavaScript #100DaysOfCode

To view or add a comment, sign in

Explore content categories