dev-resources.site
for different kinds of informations.
Hoisting with closures example
Published at
8/9/2021
Categories
javascript
closures
snippets
Author
ama
Author
3 person written this
ama
open
Try guessing what is the output of the following snippet:
function one() {
function two() {
console.log(`closure var1 - ${var1}`);
}
three();
var var1 = 'var1';
}
one();
It yields
hoisting var1 - undefined
, because of hoisting of var1
variable (it is allocated in memory with value undefined
), but it is not initialised with the value var1
by the time the closure is executed.
But, if we use setTimeout()
, by the time the callback closure function is executed var1
will have been initialised and its value is printed:
function one() {
setTimeout(function() {
console.log(`closure var1 - ${var1}`);
}, 0);
var var1 = 'var1';
}
one();
//output
closure var1 - var1
Shared with β€οΈ from Codever. Β π Β use the copy to mine functionality to add it to your personal snippets collection.
closures Article's
30 articles in total
Understanding Closures in PHP: Key Differences and Use Cases
read article
JavaScript Closures in Depth: Unlocking the Power of Lexical Scope
read article
Understanding Closures in JavaScript
read article
Mastering Closures and Decorators in Python: From Basics to Advanced
read article
Understanding Closure in JavaScript.
read article
Let's Understand JavaScript Closures: A Fundamental Concept
read article
Memoization in JavaScript
read article
Closures: Performance implications
read article
Closures: Lifting the hood
read article
A Practical Introduction to Closures in JavaScript
read article
Unlocking New Possibilities: Rust Compiler Backend Brings Closures to the .NET Universe!
read article
π¦πClosures in JavaScript visualized
read article
useClosure() {work, backwards in returnValuesAsInput (backwards, work)}
read article
Mastering Closures in JavaScript: A Comprehensive Guide
read article
# "JavaScript Closures: Demystified."
read article
Closures - JavaScript
read article
Exploring Advanced JavaScript Techniques: Closures, Prototypes, and Hoisting
read article
Unlocking JavaScript Magic: A Beginner's Guide to Closures
read article
JavaScript Closures: Understanding the Power of Functions
read article
Closures in javascript
read article
JavaScript Closure
read article
Groovy Gotchas - Loops, Closures, Scope, and Jenkins DSLs
read article
Understanding closures in JavaScript
read article
Javascript Currying and partials
read article
The ultimate explanation of closures
read article
Understanding Closures in JavaScript
read article
Closures and Memoization
read article
A Simple Explanation of JavaScript "Closures"
read article
Hoisting with closures example
currently reading
Closures in JavaScript
read article
Featured ones: