I don't undetstand why the following function doesn't return {bar:"hello"} and instead it returns undefined.
function foo2() {
return
{
bar: "hello"
};
}
I don't undetstand why the following function doesn't return {bar:"hello"} and instead it returns undefined.
function foo2() {
return
{
bar: "hello"
};
}
That's because its compiled to the below because of javascript's auto semi-colon insertion.
function foo2() {
return; // notice the semi-colon here?
{
bar: "hello"
};
}
And since return;
is called, the function terminates without going to the next line of code.
To make it work correctly just put the opening bracket right after return
as in return {
You'd be better off by using semi-colons than omitting them. Want reasons? check out Dangers of Javascript's automatic semicolon insertion