-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhoisting.js
More file actions
41 lines (30 loc) · 967 Bytes
/
hoisting.js
File metadata and controls
41 lines (30 loc) · 967 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
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Hoisiting of JavaScript mechanism where variables and function declarations are moved
to the top of their scope before the execution.
Functions and Variables declarations are hoisted but the main thing is functions are
hoiseted first and then variables.
*/
console.log(hello); //Undefined
var hello = "Hello World";
var hello;
console.log(hello); //Prints Hello World
hello = "Hello World";
car = "BMW";
var car;
console.log(car) //Prints BMW
/*
Like variables, the JavaScript engine also hoists the function declarations. And it allows us to
call functions before even writing them in our code.
*/
carDetails(); //Prints AUDI
function carDetails() {
var model = "AUDI";
console.log(model);
}
/*
Hoisting in function expressions are not allowed at all. If you doing this you will get an error.
*/
sayHello(); //Uncaught TypeError: sayHello is not a function.
var sayHello = function () {
console.log('Hello');
}