JavaScript 'hoisting'
- The global
a
is set to1
b()
is calledfunction a() {}
is hoisted and creates a local variablea
that masks the globala
- The local
a
is set to10
(overwriting the functiona
) - The global
a
(still1
) is alerted
How JS hoisting works within functions?
JavaScript hoisting within functions means that the declaration of variables are moved to the top of the function block. When you enter foo()
, var boo
is redeclared instantly even though you have not reached it (because the JS engine knows that this declaration exists within the function). Accordingly, the reason that it is undefined is because it has only been declared, you don't assign a value until the following line.
In reality, this is not a situation you should find yourself in if you declare variables in an appropriate scope and don't redeclare variables with the same name, but I understand your curiosity.
You can read more about this here.
Is there any benefit from hoisting?
The main benefit of hoisting is that functions don't have to be declared in a specific order in order to work properly. The interpreter makes a pass over a function of code and finds all the function declarations within that function and makes them available to code anywhere in the scope (hoisting them) whether the code referring to the functions is before or after where the function declarations are located. It also allows A to call B and B to call A without running into declaration ordering issues.
Variable hoisting is rarely used anymore now that we have const
and let
which are block scoped and cannot be used before declaration. So hoisting is mainly useful for function declarations now.
JavaScript Hoisting - Hoisted code after Memory Creation phase
I think the text refers to
// scope creation
var foo; // the name was declared. Thrice.
foo = undefined; // from the var
foo = function foo() { return 1 }; // from the first declaration
foo = function foo() { return 1 }; // from the second iteration
// execution
console.log(foo);
;
;
foo = 3;
where foo
is initialised with undefined
due to the var foo
declaration, but then gets overwritten by the initialisation value from the function foo
declaration which takes precedence - regardless of the order in which the declarations appeared in the code, function
declarations overrule var
declarations.
JavaScript variable hoisting behavior
In general, this is how hoisting works:
- the declaration of the variable is moved to the top
- the variable is initialized with a special "hoisted" value
- when the program reaches the
var/let/const
line, the variable is re-initialized with the value mentioned on that line (orundefined
if there's none).
Now, your example can be simplified down to this:
console.log(a)let a = 150
Hoisting inside function - inner function and variable having same name - Output?
Function and variable declarations are hoisted. Function declarations also hoist the assignment of the value.
So both function x
and var x
create a variable named x
in the current scope. function x
also assigns a function to that variable.
Assignments with =
are not hoisted.
So x = 'hi'
overwrites that function with a string.
javascript hoisting: what would be hoisted first — variable or function?
Given var foo = something
, only the variable declaration is hoisted.
This means that var foo
is hoisted but the foo = something
will run in the reading order.
Given function foo() {}
, the variable declaration and function assignment are both hoisted. This creates the variable foo
and gives it the value of the function.
If you have both of the above, then you declare the variable twice (for no extra effect) and assign it the value of the function (as that is the only assignment).
So in your second example…
The function declaration is hoisted, so f(); // - first f
calls that.
The assignment of the function expression is not hoisted, but f(); // ** - second f
appears after it in normal reading order, so the second call to foo()
hits that.
Confusion about Javascript Hoisting
The reason why var example = function()
is getting called instead of function example()
is the order of hoisting.
Functions are hoisted first, then variable declarations, per
ECMAScript 5, section 10.5 which specifies how hoisting happens.
Read more here: Order of hoisting in JavaScript
Related Topics
Difference Between JSON.Stringify and JSON.Parse
How to Do Associative Array/Hashing in JavaScript
JavaScript Drag and Drop for Touch Devices
What Is the Motivation for Bringing Symbols to Es6
Jquery Selectors on Custom Data Attributes Using HTML5
How to Get a Url Parameter in Express
How to Create a Jquery Function (A New Jquery Method or Plugin)
Remove All Event Listeners of Specific Type
Converting .Net Datetime to JSON
How to Set Locale in Datepipe in Angular 2
One Liner to Flatten Nested Object
Disable Submit Button on Form Submit
Adding 'Click' Event Listeners in Loop
How to Extend Function with Es6 Classes
How to Prevent Form from Submitting Multiple Times from Client Side