[Solved] Javascript function declaration issue [duplicate]


this is the issue about the scope where the functions has been declared.
if you run this code in browser it will work all fine, as both function and function expression are declared in global scope.

but here in fiddle it’s failing as function expression has been assigned to a global variable ‘foo2’ so it’s accessible, but function foo1 is not available in the scope in fiddle.

if you do as below, by creating variable foo2 using var, it will get create into the different scope ( no more global) and that’s why it will throw the same error as foo1.

var foo2 = function(){
  console.log("foo2 called..");
}

updated fiddle – https://jsfiddle.net/474v3mfd/6/

hope it helps.

solved Javascript function declaration issue [duplicate]