-4

In the Chrome console, I executed the following function:

function getData_working(){ 
  return [
    {Category: "Category 1", Key: "Key 10", Value: 1},
    {Category: "Category 2", Key: "Key 10", Value: 1}
  ];
}

It does work and executing getData_working() afterwards produces the expected array with two objects. However, when I execute the following function (and I've actually copied the former, only changing the placement of the brackets, to make sure it isn't a typo), I get undefined.

function getData2_failing(){ 
  return 
    [{Category: "Category 1", Key: "Key 10", Value: 1},
    {Category: "Category 2", Key: "Key 10", Value: 1}];
}

What's happening here? How does someone explain this "bug" or "feature"?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 1
    3 words: Automatic Semicolon Insertion. – elclanrs Jul 04 '16 at 17:30
  • @elclanrs In response to that I have 2 words. But I'm not supposed to spell them out. :) – Konrad Viltersten Jul 04 '16 at 17:32
  • @Xufox I'm not sure if you realize but coming up with **that** search query requires a state of insanity allowing one to even consider such a thing. It's absolutely contra-intuitive and not feasible to come up with. I regard it as a bug of the language. It's probably documented somewhere but I don't care. It's a bug. And a insane, such... – Konrad Viltersten Jul 04 '16 at 17:36

1 Answers1

4

You can't put return on its own line. Well, you can, but if you do, you will get a return value of undefined every time. This is because of how automatic semicolon insertion works.

  • Forbidden LineTerminators: The following syntactic constructs forbid a newline (“LineTerminator”) at a certain position. If there is a newline at that position, a semicolon is inserted. The ECMAScript standard calls the grammar rules below restricted productions.

PostfixExpression
LeftHandSideExpression [no LineTerminator here] ++
LeftHandSideExpression [no LineTerminator here] --

ContinueStatement
continue [no LineTerminator here] Identifier? ;

BreakStatement
break [no LineTerminator here] Identifier? ;

ReturnStatement
return [no LineTerminator here] Expression? ;

ThrowStatement
throw [no LineTerminator here] Expression? ;

For PostfixExpression, the rationale is avoiding the modification of a value on the previous line. For continue, break, return and throw, the rationale is that if they are used without an argument, they should not refer to the next line if one forgets a semicolon.

source: http://www.2ality.com/2011/05/semicolon-insertion.html

If you want the brackets on the next line, you need

return (
  [ ... ]
);

Or do like you've done in your original code

return [
  ...
];
Mulan
  • 129,518
  • 31
  • 228
  • 259
  • 1
    The person responsible for that should be shot three times in the left foot and made write the sign of semicolon in their own blood for the rest of eternity and then more. – Konrad Viltersten Jul 04 '16 at 17:33
  • @KonradViltersten Remember, the core of JavaScript was written in 10 days :) – gcampbell Jul 04 '16 at 17:34
  • @Konrad, there are many more offensive aspects of JavaScript. See also: gcampbell's comment ^_^ – Mulan Jul 04 '16 at 17:35
  • @KonradViltersten, I'm not sure why you think this is a bad thing. It is a language feature, not a deficiency. Go has this too btw. – elclanrs Jul 04 '16 at 17:37
  • @elclanrs It's contra-intuitive. It doesn't serve any apparent purpose. It's hard to trouble-shoot unless known to the developer. In fact, I'm familiar with the automatic semicolon insertion from before. Still, I got stuck on this. Might be my opinion (only shared with my network peers) but I'm kind of certain that most people get confused by this. – Konrad Viltersten Jul 04 '16 at 17:40
  • @KonradViltersten if you read the 2ality article, there's a rationale provided; tho you may disagree with it. I've copied it to this answer as well. – Mulan Jul 04 '16 at 17:41
  • @gcampbell There are other things people have done in 10 days that call for the capital punishment... (Of course, I'm kidding, fueled by fury and frustration but kidding. In fact, I do like JS. Mostly.) – Konrad Viltersten Jul 04 '16 at 17:42
  • @KonradViltersten *"It's hard to trouble-shoot unless known to the developer."* - well the [developer is responsible for knowing the tools he/she works with](http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding). *"In fact, I'm familiar with the automatic semicolon insertion from before"* - well apparently not familiar enough :( – Mulan Jul 04 '16 at 17:44
  • @naomik You're absolutely right. It's my ignorance of the ASI that got me here. Pure frustration. Too much C# lately, hehe. – Konrad Viltersten Jul 04 '16 at 17:47
  • @KonradViltersten consider this statement `if (y===0) \n return \n else \n return x/y`. Here `return` is used more like an early exit of the function. It's not a particularly useful example (I'd never write a function like this) but it might be a possibility the creators were thinking of when making this rule – Mulan Jul 04 '16 at 17:48