DEV Community

Discussion on: Hoisting in JavaScript

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Hoisting in JavaScript allows you to access variables and functions before they are created.

This is misleading. They have been created otherwise they wouldn't be accessible. They're just created first before the function body executes. Hoisting is just a convenient mental model of what is going on, but it isn't actually what is going on. It is described well here

Collapse
 
bhagatparwinder profile image
Parwinder πŸ‘¨πŸ»β€πŸ’»

When I say β€œcreated”, I’m referring to the end user looking at the source code. For them the creation has happened yet.

Internally the interpreter creates them before executing the function itself. No one is disagreeing with this.

And you are correct, it is a mental model that helps to understand this concept. The idea that the declarations are hoisted/bubbled up to the top of the source code. No one is trying to mislead.

Thanks for the feedback and linking to a more in depth article β™₯️