Why is IS `` foo: 'bar' 'a syntax error in Javascript?

My colleague wrote a line of ES6 code ...

return map(orderedContentUuids, contentUuid => { uuid: contentUuid });

As you probably can assume that he intended to return the {uuid: contentUuid } object, but since its function is with an arrow, the curly brace { actually starts a new block. (The correct code would be return map(orderedContentUuids, contentUuid => ({ uuid: contentUuid })); ).

But, unexpectedly, this code translates and runs without errors. There is no error because uuid: contentUuid seems to be evaluated before contentUuid .

You can see that if you enter the JavaScript console foo: 'bar' , it will be "bar" .

A? What's happening. Since when is this correct, JS?

+5
source share
1 answer

by email Oh. I just figured it out.

foo: 'bar' evaluated as a "label" which I did not understand was a JavaScript function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

+6
source

Source: https://habr.com/ru/post/1274240/


All Articles