Hoisting
Hosting is a default behavior in JavaScript of moving declarations at the top. While executing a code, it creates a global execution context: creation and execution. In the creation phase, JavaScript moves the variable and function declaration to the top of the page, which is known as hoisting.
Although the counter
is present in the heap memory but hasn't been initialized so, it throws an error. This happens because of hoisting, the counter
variable is hoisted here.
Here, the add
function is hoisted and initialized with undefined
in heap memory in the creation phase of the global execution context. Thus, throwing an error.
Last updated
Was this helpful?