๐ง JavaScript Variables
Variables in JavaScript are used to store data values. You can declare variables using var
, let
, and const
. Understanding how they work is key to writing bug-free and efficient code.
๐ Why Use Variables?
- To store and reuse data.
- To make code dynamic and flexible.
- To make programs more readable and maintainable.
๐ฆ Variable Declaration
There are three ways to declare variables in JavaScript:
var name = "JD"; // function-scoped let age = 25; // block-scoped const PI = 3.14; // block-scoped, read-only
๐ var
- Function-scoped.
- Can be redeclared and updated.
- Hoisted to the top of its scope and initialized as
undefined
.
๐ฆ let
- Block-scoped.
- Cannot be redeclared in the same scope.
- Hoisted but not initialized (TDZ - Temporal Dead Zone).
๐ const
- Block-scoped.
- Cannot be redeclared or reassigned.
- Must be initialized during declaration.
๐ง Scope of Variables
- Global Scope: Declared outside any block or function.
- Function Scope: Accessible only within a function (applies to
var
). - Block Scope: Declared inside a block (
{}
) and only accessible within (applies tolet
andconst
).
๐ค Hoisting
JavaScript moves declarations to the top of the scope. However, only var
is initialized with undefined
. let
and const
are hoisted but not initialized (TDZ).
console.log(a); // undefined var a = 10; console.log(b); // ReferenceError let b = 20;
โ Best Practices
- Use
const
by default. - Use
let
only if the value will change. - Avoid
var
to prevent unexpected bugs due to scope leakage.
๐งช Interview Questions
- What is the difference between
var
,let
, andconst
? - What is variable hoisting in JavaScript?
- What is the Temporal Dead Zone (TDZ)?
- Why should we prefer
const
andlet
overvar
? - What are global, function, and block scopes?
๐งฐ Practice Exercise
Write a JavaScript program to demonstrate scoping and hoisting:
function testScope() { console.log(x); // undefined var x = 5; if (true) { let y = 10; console.log(y); // 10 } console.log(typeof y); // ReferenceError } testScope();
๐ง JavaScript Variables
Variables in JavaScript are used to store data values. You can declare variables using var
, let
, and const
. Understanding how they work is key to writing bug-free and efficient code.
๐ Why Use Variables?
- To store and reuse data.
- To make code dynamic and flexible.
- To make programs more readable and maintainable.
๐ฆ Variable Declaration
There are three ways to declare variables in JavaScript:
var name = "JD"; // function-scoped let age = 25; // block-scoped const PI = 3.14; // block-scoped, read-only
๐ var
- Function-scoped.
- Can be redeclared and updated.
- Hoisted to the top of its scope and initialized as
undefined
.
๐ฆ let
- Block-scoped.
- Cannot be redeclared in the same scope.
- Hoisted but not initialized (TDZ - Temporal Dead Zone).
๐ const
- Block-scoped.
- Cannot be redeclared or reassigned.
- Must be initialized during declaration.
๐ง Scope of Variables
- Global Scope: Declared outside any block or function.
- Function Scope: Accessible only within a function (applies to
var
). - Block Scope: Declared inside a block (
{}
) and only accessible within (applies tolet
andconst
).
๐ค Hoisting
JavaScript moves declarations to the top of the scope. However, only var
is initialized with undefined
. let
and const
are hoisted but not initialized (TDZ).
console.log(a); // undefined var a = 10; console.log(b); // ReferenceError let b = 20;
โ Best Practices
- Use
const
by default. - Use
let
only if the value will change. - Avoid
var
to prevent unexpected bugs due to scope leakage.
๐งช Interview Questions
- What is the difference between
var
,let
, andconst
? - What is variable hoisting in JavaScript?
- What is the Temporal Dead Zone (TDZ)?
- Why should we prefer
const
andlet
overvar
? - What are global, function, and block scopes?
๐งฐ Practice Exercise
Write a JavaScript program to demonstrate scoping and hoisting:
function testScope() { console.log(x); // undefined var x = 5; if (true) { let y = 10; console.log(y); // 10 } console.log(typeof y); // ReferenceError } testScope();