overexchange
overexchange

Reputation: 1

Query on Execution environment of JavaScript & Python

Below diagram is retrieved after running JavaScript code(top-half) on link1 and python code(bottom-half) on link2

enter image description here

My question:

I see that names foo & bar are already sitting in global frame(in blue) of JavaScript Execution environment(EE) unlike python's EE? How did JavaScript interpreter know about these two names before starting interpretation?

Upvotes: 0

Views: 114

Answers (1)

Felix Kling
Felix Kling

Reputation: 817238

How did JavaScript interpreter know about these two names before starting interpretation?

That's something called hoisting. Before the engine executes any line of code (of a function), it looks for all variable and function declarations and creates a binding in the current environment (§10.5, steps 5 and 8). In case of variable declarations, the value is undefined because the assignment has not taken place yet.

Apparently Python doesn't work that way, but I'm not familiar enough with its inner workings to give an authoritative answer about that.

Upvotes: 2

Related Questions