Skip to content

Understanding scope in JavaScript

Last updated on January 18, 2020

This post helps you to know much about scope in JavaScript. In previous post we have seen about variable declaration.

The variable declaration is also based on scoping only worked, how? This post will answer you. Scope – Accessible region

Function scope, (var keyword)
Where variable is defined inside function body.
function createApp () {
var instance = null;
}
console.log(instance) ; // undefined

Block scope – can be while-loop, if loop ( let & const keywords)
Where variable is defined inside curly braces body.

{
let isInitialized = false;
}
console.log(isInitialized); //undefined;

If we try to access this outside of curly braces, will return as undefined.

Published inJavaScript