Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is same return type but result is difference [duplicate]

I have two function of same return type in java script but return type is difference. The using code of snipped id below

function foo1()
    {
      return {
          bar: "hello"
      };
    }

    function foo2()
    {
      return
      {
          bar: "hello"
      };
    }

calling the function..

console.log("foo1 returns:");
console.log(foo1());
console.log("foo2 returns:");
console.log(foo2());

Output the result ....

foo1 returns:
Object {bar: "hello"}
foo2 returns:
undefined 
like image 702
surendra Kandira Avatar asked May 05 '16 08:05

surendra Kandira


People also ask

Can two functions have same name but different return type?

We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types.

What happened if two methods have same name same parameters but different return types?

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.

What will happen if overloaded functions have same arguments but different return type?

Overloading with same arguments and different return type − The return type doesn't matter. If they don't have different parameters, they both are still considered as same method and a compile time error will be generated.

Can a method be overloaded based on different return type but same argument type?

Overloaded methods may have the same or different return types, but they must differ in parameters.


3 Answers

Its automatic insert of the semicolon here. The rest of code is never reached.

function foo2()
{
  return    // get a automatic semicolon insert here
  {
      bar: "hello"
  };
}

Please have a look: Warning: unreachable code after return statement

like image 170
Nina Scholz Avatar answered Oct 18 '22 23:10

Nina Scholz


That's because JavaScript interprets

return
{
    bar: "hello"
};

as return statement followed by block creation (which is ignored in runtime). Not as "return an object". And I really don't know why JavaScript devs made such decision.

Anyway ASI inserts ; after return resulting in the equivalent code:

return;
{
    bar: "hello"
};

The newline after return is the culprit. Don't use it if you wish to return something.

like image 7
freakish Avatar answered Oct 18 '22 23:10

freakish


A javascript free you do without semicolon but it puts a automatic semicolon, that is why you get undefined Refer this

function foo1()
    {
      return {
          bar: "hello"
      };
    }

    function foo2()
    {
      return{
          bar: "hello"
      };
    }
    console.log("foo1 returns:");
    console.log(foo1());
    console.log("foo2 returns:");
    console.log(foo2()); 
like image 1
nisar Avatar answered Oct 18 '22 21:10

nisar