Facebook Pixel
Block Scope with let and const: Examples

Can an inner block see a let variable from an outer block in JavaScript?

The answer

Yes. Inner blocks can access outer block-scoped variables via the scope chain. But outer blocks cannot see inner block-scoped variables. This is the same as function scope nesting.

More FAQs in Block Scope with let and const: Examples

Variables declared with let and const are only accessible inside the nearest pair of braces. This applies to if blocks, for loops, try/catch, and plain blocks. var is not block-scoped and leaks out.

Yes. Each iteration of for...of creates a fresh binding, and the loop variable does not change within an iteration. Use const for for...of and for...in. Use let only for classic for loops where the counter increments.

No. let is block-scoped, so the loop variable stays inside the loop. var is function-scoped and leaks out, which is one reason to prefer let in loops.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

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