Here's a minimal working example that I have:
function foo () {
return
1;
}
function bar () {
return 1;
}
console.log(foo()); //Undefined
console.log(bar()); //1
https://jsfiddle.net/mve3crm5/
I expect both foo()
and bar()
to return 1
.
However, foo()
returns undefined
, bar()
returns 1
.
Assuming this is expected behaviour, Why?
I don't use JavaScript often so this confuses the heck out of me. A friend recommended I use "strict mode" but I tried it and the same behaviour happens.
I tested this on Chrome and node v7.4.0, also with the node --use_strict flag.
I had my expression on a new line because the particular expression I was returning wasn't complicated but quite long. I split the expression into multiple lines and was surprised to get an undefined
value.
I debugged everything in the system and concluded there was nothing wrong except the return
statement but refused to believe it. Then, I tried to put together a minimal example and saw that it is, indeed, the case.
I am forced to believe that this is how JavaScript works, despite still being skeptical. I am partially convinced I probably have a typo somewhere, somehow.