Facebook Pixel

Do blocks create lexical environments in JavaScript?

Yes. Every block with let or const declarations creates a new lexical environment. Its outer reference points to the enclosing environment, forming part of the scope chain.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Block Scope and the Lexical Scope Chain

No. var goes into the function's variable environment, not a block environment. This is why var leaks out of blocks but stays in the function. Only let and const use block environments.

Each block's environment has an outer reference to the enclosing environment. The engine checks the block first, then follows outer references up through function and global environments until the variable is found or ReferenceError is thrown.

A function defined inside a block closes over the block's lexical environment. Even after the block ends, the function retains access to the block's variables. This is how let in a for loop fixes the closure bug.

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
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.