JS Variables
1.Javascript Variable:
a. A variable is a container that stores data values.
b. They are containers for storing data.
c. Variable is nothing but the container, where we can store
the value or data.
2.Identifier vs Variable:
a. Identifier → A name given to variable, function, or other
items.
For Example → let age; //ʼageʼ is an identifier
b. Variable → A variable is the storage location which is
identified using an identifier.
For Example → let age = 25; //ʼageʼ is a variable with the
value 25.
3.Declaring Variables in JavaScript:
a. To use a variable in JavaScript, you must first declare it.
b. This means creating a variable using specific keywords: var ,
let , or const .
c. Below is a detailed explanation of each type of declaration.
d. There are 3 declaration types in JavaScript:
i. var
ii. let
iii. const
1. var ⇒
a. The var variable is the oldest way to declare variables in
JavaScript.
b. Variables declared with var have function scope or global
scope, depending on where they are declared.
c. Variables declared with var are hoisted.
d. Disadvantage of var variable → Variable Pollution
e. For Example →
2. let ⇒
a. The let variable is used to declare block-scoped variables,
meaning the variable is accessible only within the block in
which it is defined.
b. A let variable is introduced in ES6 (2015) version.
c. Variables declared with let are hoisted, but remain in the
Temporal Dead Zone until initialization.
d. For Example →
3. const ⇒
a. The const variable declares block-scoped variables that
cannot be updated or re-declared.
b. A const variable is introduced in ES6 (2015) version.
c. Variables declared with const are hoisted, but remain in the
Temporal Dead Zone until initialization.
d. For Example →
4.JavaScript Scope:
a. JavaScript Scope is the area where a variable exists and is
accessible.
b. OR
c. In JavaScript scope refers to the accessibility and visibility
of variables, functions, and objects within different parts
of your code.
d.Types of Scopes:
i. Global Scope → Variables declared outside of
functions, blocks, or curly braces ( have a global scope.
-Global variables can be accessed from anywhere in
the program.
ii. Local Scope or Function Scope → Variables declared
inside a function have local (or function) scope.
-These variables cannot be accessed outside the
function where they are declared.
-Note: Variables declared within the function have a
function scope.
iii. Block Scope → Introduced in ES6 with the let and
const keywords.
-Variables declared inside curly braces ( {} ) can only
be accessed within that block.
-Note: Variables declared with JS Variables var do not
have block scope, meaning they are still accessible
outside the block.
iv. Module Scope (ES6) → Variables declared in ES6
modules (using import and export) are scoped to the
module.
-They are not accessible outside the module unless
explicitly exported.
-Each module has its own scope, and variables and
functions declared within a module are private to that
module unless exported.
v. Script Scope → The variables which are directly
present inside the JS files.
5.Variable Hoisting:
a. Hoisting means we can initialize the variable before its
declaration.
b. The JS engine allows programmer to access the member
before its declaration is known as Hoisting.
c. OR
d. Hoisting means JavaScript moves declarations to the top of
the scope before code execution.
e. Only declarations are hoisted, not initializations.
f. NOTE → In JS variable hoisting is possible in var variable
only.
-Variables declared with let and const are also hoisted but
remain in the Temporal Dead Zone (TDZ) until initialization.
6.Temporal Dead Zone (TDZ):
a. The Temporal Dead Zone TDZ is the period of time in
JavaScript where a variable is declared using let or const
but cannot be accessed because it hasnʼt been initialized
yet.
b. If you try to access the variable before it is initialized, you
get a ReferenceError .
7.Comparison Between var vs let vs const
a. Create the comparison table whatever we have discussed in
the class or create the table that you have taken the
picture in the class.