forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhoisting.js
More file actions
32 lines (23 loc) · 928 Bytes
/
Copy pathhoisting.js
File metadata and controls
32 lines (23 loc) · 928 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
/*
Hoisting: Before executing any code, the javscript engine sets up memory for variables and functions.
variables are assigned undefined by the engine.
*/
// Example 1
x = 5; // Assign 5 to x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x; // Display x in the element
var x; // Declare x
// Example 2
var x; // Declare x
x = 5; // Assign 5 to x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x; // Display x in the element
// ________________________________________________________
/* Conclusion :Example 1 gives the same result as Example 2
so, Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope
(to the top of the current script or the current function)
*/
console.log(foo);
const foo = "foo"; // unable to print due to the hoisting behaviour
console.log(ok);
var ok = "this is printing";// var is hoisted