Scope of a Javascript variable is its execution context, which is either the function in which the variable is declared or - for variables declared outside a function - global. There is no block level scope in Javascript - a variable declared inside a block is scoped at enclosing function (or script) level. Having said that, consider below code:
var x = 10; // This is global scope
function example(cond) {
if(cond)
{
var x = 20;
}
return x;
}
console.log(example(true));
console.log(example(false));
What is output of above?