Understanding Implicit ECMAScript Semicolons and Parsing Spaces

I saw, very often in fact, it is quoted as saying why use the K & R style when writing ECMAScript.

function foo () {
  return
    {
      foo: 1
    }
  ;
}

This does not work in ECMAScript or Javascript: merging with an implicit semicolon results in a function return undefined. However, I see it all the time too

function bar () {
  var a = "BAR";
  return a
    .toLowerCase()
  ;
}

And I wonder why implicit semicolons do not result in a return "BAR", why does it return bar?

+3
source share
1 answer

Because the syntax does not work with an implicit semicolon at the end of a line.

If you add a semicolon:

function bar () {
  var a = "BAR";
  return a;
    .toLowerCase()
  ;
}

You will get a syntax error on the next line.

+2
source

All Articles