Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the JavaScript syntax foo: mean?

Tags:

javascript

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.

like image 916
dzilbers Avatar asked Mar 11 '23 01:03

dzilbers


1 Answers

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++;
}
like image 51
Aᴄʜᴇʀᴏɴғᴀɪʟ Avatar answered Mar 20 '23 20:03

Aᴄʜᴇʀᴏɴғᴀɪʟ