Scope

In JavaScript scope is kept within the function, but not within the blocks like if, while and for statements.

In simple language, It means variable created inside the function it is only accessible to that particular function and variable created inside the if condition, while and for loop statements are accessible to it’s to outside blocks like in it’s parent function scope.

Yes, But it is strange.

Two types of scope:

  1. Global Scope
  2. Function Scope
var scope = "JavaScript Hive";
// Variable scope is created with "JavaScript Hive" value.

if (true) {
    var scope = "Hive";
    // Once again scope variable is created,
    // But it's already created in it's parent scope so it will overridden.
    // So now the value of the scope is "Hive"
}

alert(scope == "Hive"); // true
alert(scope == "JavaScript Hive"); // false

In above example, Scope variable is created with the value “JavaScript Hive”, after that in if condition (which is always right) we re-created the scope variable with the new value “Hive”, But JavaScript does not allow this type of scope so it will be override the value and the value of scope changed to the “Hive”.

So we can say that JavaScript can only support the Function scope.

var scope = "JavaScript Hive";
// Variable scope is created with "JavaScript Hive" value.

function testing() {
    var scope = "Hive";
    // "testing()" is function so JavaScript will create the
    // new scope variable for this function
}

testing();
// Called the testing() function.

alert(scope == "Hive"); // false
alert(scope == "JavaScript Hive"); // true

In above example, I created the new function called the “testing()”, and I am re-created the variable scope inside the testing() function, So what’s happen is here that, JavaScript only support the function scope so in testing() brand new “scope” variable is created which no relation with the it’s parent variable “scope”. So the value of the parent’s scope variable is not changed.

Global variable

Variable which not created inside any of the function means It is created in main global function or the outside of the function or between the script tag it is called the global variable.

Global variable is available to all the function, and it is also accessed using the window object, because indirectly it will become the property of the window object.

var scope = "JavaScript Hive";

alert(window.scope == "JavaScript Hive"); // true

Declare variable using var keyword

It is good practice to declare the variable always with the var keyword. It will helpful us for the memory management.

Variable created using the var keyword inside the function, it will not be accessible outside the function.

var var1 = "var1";

function testing() {
    var var2 = "var2";
}

testing();

alert(var1); // var1
alert(var2); // Uncaught ReferenceError: var2 is not defined

In above example, I created the var2 variable inside the testing() function, So it will not be accessible outside the testing() function, due to the JavaScript only support function scope.

When I tried to accessed the var2 variable outside the function than I got the ReferenceError (var2 is not defined).

Declare variable without the var keyword.

If we declare the variable without the var keyword than it will be become the property of the window object, Yes it’s sound crazy but it’s correct.

Even when you create the variable inside the function without the var keyword, than it become the property of the window object and due to this it will be accessible outside the function also.

So here memory is rapidly increase and it will be become the reason of the Memory Out Of Exception.

var var1 = "var1";

function testing() {
    var2 = "var2";
}

testing();

alert(var1); // var1
alert(var2); // var2

So I strongly recommend you to create the variable always using the var keyword.

I hope you enjoy this article, Please comment down your review about this,

Happy Coding!!!

🙂