1

Take a look into the below code and explain me why it returns undefined rather than the variable.

function aaa() {
  return
  {
    test: 1
  };
}
console.log(aaa());
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Kannan T
  • 1,639
  • 5
  • 18
  • 29
  • 3
    return is a statement. If nothing follows after it on the same line, it returns undefined – Rajesh Sep 27 '17 at 07:39
  • 1
    This will help: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return#Automatic_Semicolon_Insertion – Rajesh Sep 27 '17 at 07:42
  • @JJJ I rolled back the edit but the question was marked as dupe at the same time. That caused the issue. Have again rolled back the edit. – Rajesh Sep 27 '17 at 07:45
  • @PatrickRoberts I was not trying to fix anything. I improved an edit with fixing format of snippet. My intention was not fixing it. – bennygenel Sep 27 '17 at 07:46

2 Answers2

4

That's because of the new line at the end of return (Automatic Semicolon Insertion).

Try:

function aaa() {
  return {
     test: 1
  };
}
console.log(aaa());
CD..
  • 72,281
  • 25
  • 154
  • 163
  • 1
    Just a pointer, for such questions, please look for a similar post and mark it as duplicate. Answering a duplicate is a bad practice. – Rajesh Sep 27 '17 at 07:42
  • @Rajesh I couldn't find the original quesiton buddy or else i wouldn't have asked – Kannan T Sep 27 '17 at 07:50
  • @kannan Above comment was not for you. It was intended for CD.. Point was, the explanation for this question was easy and so people choose to answer instead of looking for duplicates. Which is wrong. If you know the solution, it more likely that you can phrase the search query better to find appropriate duplicate. – Rajesh Sep 27 '17 at 07:53
4

Because you made a newline:

function aaa() {
  return {
    test: 1
  };
}
console.log(aaa());
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • 2
    Just a pointer, for such questions, please look for a similar post and mark it as duplicate. Answering a duplicate is a bad practice. – Rajesh Sep 27 '17 at 07:42