Sami Rahman
Sami Rahman

Reputation: 55

Function return undefined value when function operation done?

After call getObject() function return undefined value.

function getObject(a)
{
    return
    {
        x : a
}
}

console.log(getObject());

Upvotes: 3

Views: 83

Answers (2)

Stasel
Stasel

Reputation: 1298

JavaScript does not support line breaks except when you are specifying JSON:

function getObject(a)
{
    return {
        x : a
     }
}

console.log(getObject());

This is working for me. Note the first { is in the same line as the return statement

Upvotes: 2

Md Mehedi Hasan
Md Mehedi Hasan

Reputation: 1792

After call getFunction() function return "undefined" value.

 function getFunction(a)
    {
        return
        {
            x : a
    }
    }

    console.log(getFunction ());

In come case JavaScript insert automatic semicolon after some specified place according to ECMA-262. Please take a details look here- http://www.ecma-international.org/ecma-262/5.1/#sec-7.9

Now your code show look like this-

function getFunction (a)
    {
        return  ;
         {
            x : a
         }
    }
    console.log(getFunction ());

You may correct your code in following way-

function getFunction (a)
    {
        return{
            x : a
    }
  }
  console.log(getFunction ());

Upvotes: 2

Related Questions