Facebook Pixel

What is the difference between function scope and block scope in JavaScript?

Function scope means a variable is visible throughout the function (var). Block scope means a variable is visible only within the nearest enclosing block (let and const). var ignores block boundaries; let and const respect them.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in What Is Scope in JavaScript?

Scope is the set of rules that determines where variables are accessible. JavaScript has global scope, function scope, block scope, and module scope.

Yes. let and const are block-scoped. They are only accessible within the nearest enclosing pair of braces. var is function-scoped and ignores block boundaries (except function bodies).

In ES modules, top-level variables are scoped to the module, not global. They are only accessible outside if explicitly exported. This prevents accidental global pollution.

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.