Why does the following piece of code not define the dummy function?
if(function dummyFunction() {}){
}
console.log(dummyFunction); // Throws ReferenceError: dummyFunction is not defined
The reason why I ask is - had I defined it as following -
function dummyFunction(){}
console.log(dummyFunction); //This would have worked
BUT:
var xxx = function dummyFunction(){}
console.log(dummyFunction); //This would NOT have worked.
Somehow I feel that when I use it with if
condition, it should have behaved like the first case and not the second. I fail to understand why without assignment to a variable (like in the second case) I am not able access the method via function name.
Update: I got to face this problem when I was solving a JS questionnaire.