Skip to content

Hoisting: Function & Variables Declaration act as a priority stack #1913

@eulier1

Description

@eulier1

Yes, I promise I've read the Contributions Guidelines (please feel free to remove this line).


"I already searched for this issue":

Edition: (2nd)

Book Title: Scope and Closures

Chapter: 5

Section Title: Hoisting : Yet Another Metaphor

Question:

Given the following code

greeting();

function greeting() {
    console.log(`Hello there!`);
}

var greeting;

What would be the value of greeting?.

It turns out my intuition was not right. Which is greeting undefined. But When I evaluate at the Dev Tools it, runs the function.

The observable behavior is like the JS Compiler holds identifier names references like a stack for functions and variables definitions, that match the order you describe in the Hoisting: Yet Another Metaphor, last secction. Function definitions are hoisted first and have more priority than variable definitions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions