In this blog I will be writing about one more misconception that developers generally carry in their mind when they work with javascript. I have seen a lot of javascript developers declaring variables inside blocks(between those curly braces we use for if/else or while or for loop etc.). I think the only possible reason behind this approach may be - "they are unaware of the scope of variables used in javascript".
If you might have been working with some language like C or Java or might be some other object oriented language, you surely might be knowing that every variable/function has a scope of execution.
Take a look on the below piece of code:
If you might have been working with some language like C or Java or might be some other object oriented language, you surely might be knowing that every variable/function has a scope of execution.
Take a look on the below piece of code:
function blockScopeTest(){
if (true)
{
var blockLevel = "I am in first if block";
}
if (true)
{
console.log(blockLevel); //variable 'blockLevel' is visible here.
}
}
if (true)
{
var blockLevel = "I am in first if block";
}
if (true)
{
console.log(blockLevel); //variable 'blockLevel' is visible here.
}
}
When you see this, you might think that since the variable is declared and initialized in the first if block, while we are trying to access it in second if block, therefore this will result in "ReferenceError: blockLevel is not defined".
But unfortunately this is not correct.
To your surprise the second if block will successfully print "I am in first if block" on console!!!!
This is because unlike many other languages javascript does not have any block level scope.
It is therefore suggested to always declare variables at the top of the function and not inside blocks.
Even when you are declare a var statement inside a for or in for/in loop, The variable is not only visible in that loop, rather it will be visible in the whole function indeed in which the loop exists.
But unfortunately this is not correct.
To your surprise the second if block will successfully print "I am in first if block" on console!!!!
This is because unlike many other languages javascript does not have any block level scope.
It is therefore suggested to always declare variables at the top of the function and not inside blocks.
Even when you are declare a var statement inside a for or in for/in loop, The variable is not only visible in that loop, rather it will be visible in the whole function indeed in which the loop exists.
function blockScopeTest(){
var count = 5;
for(var count = 0; count<10; i++)
{
}
if(true)
{
console.log(count)//what should be the value of count?
//Surprisingly the value will be 10 ,not 5
}
}
var count = 5;
for(var count = 0; count<10; i++)
{
}
if(true)
{
console.log(count)//what should be the value of count?
//Surprisingly the value will be 10 ,not 5
}
}
Note - Javascript does not have block level scope but have function level scope. Therefore variable declared in one function is not visible in another function.