-1

I have 2 identical functions. But they return 2 different things..

function fun1()
{
  return {
      obj: "hello"
  };
}

function fun2()
{
  return
  {
      obj: "World..!"
  };
}

So, when i add something to print out the functions,

console.log("fun1 returns:");
console.log(fun1());
console.log("fun2 returns:");
console.log(fun2());

My O/p is:

fun1 returns:
Object {obj: "hello"}
fun2 returns:
undefined 

Why does this happen. Is this a structural mistake?? How do i get to make the 2nd function print out identical o/p??

Matt
  • 171
  • 3
  • 13
  • 2
    When you break out the `{` in `fun2` onto a different line, there is an automatic `;` added after your `return` statement, so it really evaluates out to: `function fun2() { return; }` – Anthony Forloney Jan 25 '15 at 00:12

1 Answers1

0

Because javascript inserts a ; right after return in the func2 function.

potashin
  • 44,205
  • 11
  • 83
  • 107
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66