-1

Look at this screenshot:

enter image description here

I first made a mistake. I know that arrow function can return a value without using the "return" keyword, like f=()=>3. I thought that I can use this compact syntax to return an object, like f=()=>{a:3}. After trying it, I realised that "{a:3}" in this case is interpreted as the function body, not object literal. Further trying f=()=>{a:3; return 'three'} confirmed this: JS interpreted what inside {} as function body, and "a:3" not a property but a statement.

My question is then:

What statement is a:3;? What's this syntax? What does it do? I cannot find any reference about it. Very strange...

Bing Ren
  • 1,589
  • 17
  • 26
  • Related: [ECMAScript 6 arrow function that returns an object](https://stackoverflow.com/q/28770415) – VLAZ Jun 30 '21 at 05:16
  • 1
    Please, post text as text, not as photographs of text. This is a website for programmers, not photographers. We want to copy&paste&run your code, copy&paste your inputs, read your outputs, and copy&paste&google your error messages, not critique your use of color and perspective. https://meta.stackoverflow.com/a/285557/2988 https://idownvotedbecau.se/imageofcode – Jörg W Mittag Jun 30 '21 at 05:22

1 Answers1

1

It's a label followed by an unused expression.

f=()=>{
  a: // label
  3 // expression
}

A label means nothing in this case, but it isn't forbidden by the JS syntax (unfortunately).

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320