Facebook Pixel

When is a lexical environment created in JavaScript?

Every time the engine enters a new scope: once for the global scope, on each function call, and for each block (pair of braces) containing let or const declarations.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Lexical Environment in JavaScript Explained

An internal data structure that manages scope. It consists of an environment record (the variable bindings) and a reference to the outer (parent) lexical environment. The engine creates one for each scope.

It means the outer environment reference is determined by where the code is written in the source, not where the function is called. This is why JavaScript is called lexically (statically) scoped.

In the spec, the variable environment holds var and function declarations, while the lexical environment holds let and const. In practice they are often the same object, but the distinction matters in edge cases like catch blocks.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0