4

what does the following code meaning? (it is not json - it is code which does not generate error by js interpreter)

foo: 5

The reason for the question is as follows. In the arrow function examples there is one that shows the confusion between json and code block syntax:

var func = () => { foo: 1 };

The func() returns undefined and the above code does not fail. I tried to put just the foo: 5 code as the only code in a js module - and it works... I do not know about a ':' operator neither about labels in js.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
dzilbers
  • 322
  • 1
  • 3
  • 9

1 Answers1

5

It's a JavaScript label: documentation here.

You can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

Note that JavaScript has NO goto statement, you can only use labels with break or continue.

Example usage (from MDN)

var itemsPassed = 0;
var i, j;

top:
for (i = 0; i < items.length; i++){
  for (j = 0; j < tests.length; j++) {
    if (!tests[j].pass(items[i])) {
      continue top;
    }
  }

  itemsPassed++;
}
Community
  • 1
  • 1
  • As per quoted documentation, JS label is relevant for loop only. In my example there is no loop following the label. But it's still legal code. – dzilbers Oct 25 '16 at 12:36
  • @dzilbers if you keep reading the documentation, it states that you can use blocks (section: "Using a labeled block with break") and even functions. The Javascript parser will expect an expression to follow the `label`. Unless you're in strict mode, in which case it will limit what can follow. – Aᴄʜᴇʀᴏɴғᴀɪʟ Oct 25 '16 at 13:27
  • I have looked now into MDN and into ECMA specs about labelled statement. It is strange (why labels that are not adjacent to a loop or to a block are acceptable at all if there is no any reason for their usage?) but it is true... – dzilbers Oct 25 '16 at 16:49