This question already has answers here:
Function declarations precedence/overwriting variable declarations? Hoisting? Why?
(2 answers)
JavaScript hoisting for multiple declarations of the same variable
(1 answer)
Order of hoisting in JavaScript
(2 answers)
Closed 4 years ago.
I'm reading the second book of the series "You don't know JS" and I've read that functions are hoisted before variables.
So this is the code:
foo(); // 1
var foo;
function foo() {
console.log( 1 );
}
foo = function() {
console.log( 2 );
};
The output of this will be 1. But why? Functions are hoisted first and then variables. So after my function foo (the one that prints 1) is hoisted it has to be followed by the variable foo. So the result should be "undefined" instead of "1".
I expect the code to behave as if it had been:
// hoisted first
function foo() {
console.log( 1 );
}
// hoisted second
var foo; // implicitly initialized to 'undefined'
foo(); // call 'undefined' - error?
foo = function() {
console.log( 2 );
};
What's happening here?
As said, functions are hoisted before variables; if the interpterer comes across var foo after foo has already been defined in the scope, it will simply be ignored. It doesn't assign foo to undefined, it only ensures that a variable named foo exists in the current scope - which it already does.
As it says in your You Don't Know JS link:
multiple/duplicate var declarations are effectively ignored
Here's another commonly seen example of hoisted duplicate variables being ignored that might be more familiar/intuitive:
if (false)
var foo = false;
else
var foo = true;
turns into
var foo;
var foo; // does not actually do anything; does not assign `undefined` to `foo`, ignored
// foo just *happens* to not have anything assigned to it in almost all cases like this
if (false)
foo = false;
else
foo = true;
Related
This question already has answers here:
Javascript named function as an expression
(1 answer)
Javascript functions like "var foo = function bar() ..."?
(9 answers)
Closed 3 years ago.
when we declare a named function let's say in google chrome console and if we call it then the contents of the function are getting executed. how ever if we create a reference for that function and if I am trying to call that function with which it is named, I am getting ReferenceError.
function foo() {
console.log("something");
}
foo(); // will print out "something"
var x = function bar() {
console.log("something x");
}
x(); //will print "something x"
bar(); //throws ReferenceError, bar is not defined.
In the above code function bar(){...} when assigned to a variable x acts as function expression not function declaration .The function bar becomes local variable to x. According to MDN
a name can be provided with a function expression and can be used inside the function to refer to itself,
var x = function bar() {
console.log(bar)
console.log("something x");
}
x();
This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 5 years ago.
My sample code:
var foo = 1;
function bar() {
foo = 10;
return;
function foo() {}
}
bar();
alert(foo);
Why does it alert 1? I am expecting 10.
However if I comment out function foo(){} after return, it does return 10.
Function declarations inside other functions are treated as if they appeared at the very start of the containing function. That is, your bar() function is treated as if it looked like:
function bar() {
function foo() {}
foo = 10;
return;
}
Thus foo is a local symbol in the function, and assignment to it does not affect the global foo.
This question already has answers here:
What are the precise semantics of block-level functions in ES6?
(2 answers)
Closed 6 years ago.
I read Kyle's I don't know JS and came to know that function declarations will hoist first before the var. So in the below code
<script>
foo();
var a = true;
if ( a ) {
function foo() { console.log( "a" ); }
}else {
function foo() { console.log( "b" ); }
}
foo();
</script>
foo should be hoisted first and should print "b" as output for the first foo(), right? or am I missing anything?
Explanation or a link to understand to the code and hoisting in different scenario's would be a great help.
Hoisting (in the way that you're expecting here) will only work when there is a precedential order of things that is unambiguous. In this case, because your function is defined inside an if block, hoisting it will not happen as you expect it to.
This will work:
foo();
var a = true;
function foo() { console.log( "a" ); }
foo();
Because it removes from the equation your if statement.
This question already has answers here:
Why does an undefined variable in Javascript sometimes evaluate to false and sometimes throw an uncaught ReferenceError?
(8 answers)
Closed 7 years ago.
I understand the following function because variable hoisting happens.
foo();
function foo() {
console.log( a ); // undefined
var a = 2;
}
However what I don't understand is the following part.
I got Reference Error, why?
foo()
function foo() {
console.log( a ); // Reference Error
a = 2;
}
--- Edit ---
So far, what I understand from the answers is the second does not make any hoisting and we cannot use any undefined variable.
foo()
function foo() {
// we cannot use any undefined variable, which "a" here
console.log( a );
window.a = 2;
}
For example
var a;
a; //undefined
b; //Reference error
In the first one, a is declared, but undefined, in the second it's also also undeclared (can't be found in foo or in global scope), so throws a ReferenceError.
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Javascript: var functionName = function() {} vs function functionName() {}
What is the difference between a function expression vs declaration in Javascript?
Today I stumbled upon the following phenomenon:
foo();
bar();
function foo()
{
console.log("inside foo");
}
var bar = function()
{
console.log("inside bar");
}
FireBug complains with the following error message:
bar is not a function
Several tutorials claim that function f() and var f = function() are basically the same thing. Evidently, they are not, but what exactly is going on here?
Function declarations are available anywhere in the scope they're defined in, even before their physical definitions.
var bar = function() { ... }; is a normal variable that happens to hold a function. Like all other variables, it can only be used after its assigned.
(You cannot observe the future value of a variable)