SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
VARIABLE
HOISTING
Variable Hoisting
Variable Hoisting is a behaviour in Javascript
where variable declaration are moved to the top of
the scope before execution
function hoist() {
a = 20;
var b = 100;
}
hoist();
console.log(a); // Accessible as a global
variable outside hoist() function. Output: 20
console.log(b); // Since it was declared, it is
confined to the hoist() function scope. We can't
print it out outside the confines of the hoist()
function. Output: ReferenceError: b is not defined
Hoisting and var
The var variable is scoped to the function it sits
within, or the global scope if it isn’t within a
local function.
Global Variable,
console.log(hoist); // Output: undefined
var hoist = 'The variable has been hoisted.';
Function Variable,
function hoist() {
console.log(message);
var message='Hoisting is all the rage!'
}
hoist(); // Output: undefined
Hoisting and let
The let variable is a new form of variable
introduced in ES2015. Instead of being scoped to
the nearest function, let is scoped to the nearest
block.
console.log(hoist); // Output: ReferenceError:
hoist is not defined
let hoist = 'The variable has been hoisted.';
Declare the variables first,
let hoist;
console.log(hoist); // Output: undefined
hoist = 'Hoisted'
Hoisting and const
The const keyword was introduced in es6 to allow
immutable variables. That is, variables whose
value cannot be modified once assigned.
console.log(hoist); // Output: ReferenceError:
hoist is not defined
let hoist = 'The variable has been hoisted.';
Globally,
const PI;
console.log(PI); // Output: SyntaxError: Missing
initializer in const declaration
PI=3.142;
So are let and const variables not hoisted?
All declarations are hoisted in javascript, while
the var declarations are initialized with
undefined, but let and const declarations remain
uninitialized.
● They will be only initialized when their
lexical block is evaluated by engine.
● Making it inaccessible before it is evaluated.
● This inaccessible time span between creation
and initialization is called “Temporal Dead
zone”.
So, answer to the question, let and const hoist
but cannot access them before the actual
declaration is evaluated

More Related Content

PDF
Building beautiful apps using google flutter
PPTX
Dart and Flutter Basics.pptx
PPTX
Dart programming language
DOC
Asp.Net Tutorials
PDF
Flutter Online Study jam 10-7-2019
PPTX
react-slides.pptx
PDF
Introduction to Robot Framework
PDF
An Introduction of Node Package Manager (NPM)
Building beautiful apps using google flutter
Dart and Flutter Basics.pptx
Dart programming language
Asp.Net Tutorials
Flutter Online Study jam 10-7-2019
react-slides.pptx
Introduction to Robot Framework
An Introduction of Node Package Manager (NPM)

What's hot (20)

PDF
Introduction to GPU Programming
PDF
BKK16-110 A Gentle Introduction to Trusted Execution and OP-TEE
PDF
Page Object Model and Implementation in Selenium
PPTX
Flutter workshop
ODP
Testes pythonicos com pytest
PDF
Scalable Django Architecture
PPTX
C sharp
PDF
Xampp Workshop
PDF
PPTX
Dart ppt
PPT
Eclipse introduction IDE PRESENTATION
PDF
Best node js course
PDF
Flutter101
PPSX
PPT
Asp.net control
PDF
Introduction to back-end
PPTX
React JS - A quick introduction tutorial
PPTX
Introduction to GPU Programming
BKK16-110 A Gentle Introduction to Trusted Execution and OP-TEE
Page Object Model and Implementation in Selenium
Flutter workshop
Testes pythonicos com pytest
Scalable Django Architecture
C sharp
Xampp Workshop
Dart ppt
Eclipse introduction IDE PRESENTATION
Best node js course
Flutter101
Asp.net control
Introduction to back-end
React JS - A quick introduction tutorial
Ad

Similar to Variable hoisting in JavaScript (20)

PPTX
Javascripts hidden treasures BY - https://geekyants.com/
PDF
Important JavaScript Concepts Every Developer Must Know
PDF
Java scriptconfusingbits
PDF
Java scriptconfusingbits
PPTX
Weird Javascript Weekends first session presentaion
PDF
1669958779195.pdf
PPTX
Modern JS with ES6
PPTX
Modern JS with ES6
PPTX
Object oriented java script
PDF
Web 4 | Core JavaScript
PPTX
ECMAScript 2015
PPT
Web development basics (Part-4)
PPTX
JS knowing-nuances
PPTX
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
PDF
JavaScript Interview Questions 2023
PDF
Brief Introduction on JavaScript - Internship Presentation - Week4
PPTX
Final ppt
PPTX
Episode 15 - Basics of Javascript
PPT
9781305078444 ppt ch02
Javascripts hidden treasures BY - https://geekyants.com/
Important JavaScript Concepts Every Developer Must Know
Java scriptconfusingbits
Java scriptconfusingbits
Weird Javascript Weekends first session presentaion
1669958779195.pdf
Modern JS with ES6
Modern JS with ES6
Object oriented java script
Web 4 | Core JavaScript
ECMAScript 2015
Web development basics (Part-4)
JS knowing-nuances
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
JavaScript Interview Questions 2023
Brief Introduction on JavaScript - Internship Presentation - Week4
Final ppt
Episode 15 - Basics of Javascript
9781305078444 ppt ch02
Ad

More from Ideas2IT Technologies (20)

PDF
Version comaparison in JavaScript
PDF
Currying in JavaScript
PDF
JS Testing Frameworks
PDF
Cool usage of Encoding and Decoding a URI in Javascript
PDF
Iterables and Iterators in JavaScript
PDF
String comparison in javascript
PDF
JavaScript symbols
PDF
Json.parse() in JavaScript
PDF
Bubble sort in Java Script
PDF
Performance analysis in merging arrays - JavaScript
PDF
Nullish coalescing in JavaScript
PDF
Conditionally add keys in JavaScript
PDF
What is Big O in JavaScript - Part-1
PDF
Formidable ES6 spread operator in JavaScript
PDF
Logging in JavaScript - Part-5
PDF
Logging in JavaScript - Part-4
PDF
Logging in JavaScript - Part-3
PDF
Logging in JavaScript - part-2
PDF
Logging in JavaScript - part-1
PDF
Array vs set in JavaScript
Version comaparison in JavaScript
Currying in JavaScript
JS Testing Frameworks
Cool usage of Encoding and Decoding a URI in Javascript
Iterables and Iterators in JavaScript
String comparison in javascript
JavaScript symbols
Json.parse() in JavaScript
Bubble sort in Java Script
Performance analysis in merging arrays - JavaScript
Nullish coalescing in JavaScript
Conditionally add keys in JavaScript
What is Big O in JavaScript - Part-1
Formidable ES6 spread operator in JavaScript
Logging in JavaScript - Part-5
Logging in JavaScript - Part-4
Logging in JavaScript - Part-3
Logging in JavaScript - part-2
Logging in JavaScript - part-1
Array vs set in JavaScript

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Digital Strategies for Manufacturing Companies
PDF
AI in Product Development-omnex systems
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
history of c programming in notes for students .pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Introduction to Artificial Intelligence
PDF
Nekopoi APK 2025 free lastest update
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Softaken Excel to vCard Converter Software.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PTS Company Brochure 2025 (1).pdf.......
Digital Strategies for Manufacturing Companies
AI in Product Development-omnex systems
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Operating system designcfffgfgggggggvggggggggg
history of c programming in notes for students .pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Introduction to Artificial Intelligence
Nekopoi APK 2025 free lastest update
VVF-Customer-Presentation2025-Ver1.9.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia
Which alternative to Crystal Reports is best for small or large businesses.pdf

Variable hoisting in JavaScript

  • 2. Variable Hoisting Variable Hoisting is a behaviour in Javascript where variable declaration are moved to the top of the scope before execution function hoist() { a = 20; var b = 100; } hoist(); console.log(a); // Accessible as a global variable outside hoist() function. Output: 20 console.log(b); // Since it was declared, it is confined to the hoist() function scope. We can't print it out outside the confines of the hoist() function. Output: ReferenceError: b is not defined
  • 3. Hoisting and var The var variable is scoped to the function it sits within, or the global scope if it isn’t within a local function. Global Variable, console.log(hoist); // Output: undefined var hoist = 'The variable has been hoisted.'; Function Variable, function hoist() { console.log(message); var message='Hoisting is all the rage!' } hoist(); // Output: undefined
  • 4. Hoisting and let The let variable is a new form of variable introduced in ES2015. Instead of being scoped to the nearest function, let is scoped to the nearest block. console.log(hoist); // Output: ReferenceError: hoist is not defined let hoist = 'The variable has been hoisted.'; Declare the variables first, let hoist; console.log(hoist); // Output: undefined hoist = 'Hoisted'
  • 5. Hoisting and const The const keyword was introduced in es6 to allow immutable variables. That is, variables whose value cannot be modified once assigned. console.log(hoist); // Output: ReferenceError: hoist is not defined let hoist = 'The variable has been hoisted.'; Globally, const PI; console.log(PI); // Output: SyntaxError: Missing initializer in const declaration PI=3.142;
  • 6. So are let and const variables not hoisted? All declarations are hoisted in javascript, while the var declarations are initialized with undefined, but let and const declarations remain uninitialized. ● They will be only initialized when their lexical block is evaluated by engine. ● Making it inaccessible before it is evaluated. ● This inaccessible time span between creation and initialization is called “Temporal Dead zone”. So, answer to the question, let and const hoist but cannot access them before the actual declaration is evaluated