Hoisting in JavaScript
If you are learning JavaScript and wondering what on earth is hoisting, read on.
Hoisting is simply pulling a variable/ named function to the top and have them be available to the entire code. Before running all the code, the browser first gets all declarations, hoisting is the name of this process.
Let’s look at an example:
Example 1:
console.log(d);var d;//undefined
The reason that d is undefined is because d has been declared but not been assigned to a value. Here, d has been hoisted to the top.
If we assign d to a value at the top before the console log, it will print out the value of d which in this case is 4.
d = 4console.log(d);var d;//4
That, in a nutshell, is hoisting.
Happy coding!