-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhoisting.js
More file actions
28 lines (24 loc) · 743 Bytes
/
hoisting.js
File metadata and controls
28 lines (24 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// What is function hoisting in JavaScript?
var foo = function foo(){
return 12;
};
// In JavaScript, variable and functions are hoisted.
// Let's take function hoisting first. Basically,
// the JavaScript interpreter looks ahead to find
// all variable declarations and then hoists them
// to the top of the function where they're declared. For example:
foo(); // Here foo is still undefined
var foo = function foo(){
return 12;
};
// Behind the scene of the code above looks like this:
var foo = undefined;
foo(); // Here foo is undefined
foo = function foo(){
// Some code stuff
}
var foo = undefined;
foo = function foo(){
// Some code stuff
}
foo(); // Now foo is defined here