Local variable oopsGlobal
, gives an error when used outside of function fun1
but the other variable name
defined in the same way does not give an error and is accessible outside of the function?
Am I missing something here?
function functionName() {
// variables created without Var always global scope
var name = "sudeep";
console.log("myfirst function");
}
function functionName1() {
// variables created without Var always global scope
console.log("Local variable with var:" + name);
}
// Declare your variable here
var myGlobal = 10;
function fun1() {
// Assign 5 to oopsGlobal Here
var oopsGlobal = 5; //local scope to variable when accessed outside gives error so working fine
}
functionName();
functionName1();
console.log(name); // **does not give error at all**
fun1();
console.log(name); //**does not give error at all**
console.log(oopsGlobal); //give error so works fine