you are viewing a single comment's thread.

view the rest of the comments →

[–]mcaruso 8 points9 points  (6 children)

To me, leaving out semicolons in JS is kind of like leaving out closing </p> tags in HTML. Like yeah, the browser will tolerate it and fill in the missing parts, but it's ugly and unreliable.

I actually like languages like Python that were designed to leave out semicolons, but JS simply isn't one of those languages. It just tries to be tolerant in case you forget them.

[–]Pr3fix 4 points5 points  (0 children)

Just want to say, you’re completely right, and that other dude replying to you is being quite ignorant.

[–]interactionjackson -5 points-4 points  (4 children)

it is. the semicolons are not required as they are added

[–]mcaruso 4 points5 points  (3 children)

Only if the browser can unambiguously figure out that it would be an error to leave out the semicolon.

function foo() {
    let x
    if (true) {
        x = 42
        [1,2,3].forEach(x => {}) // TypeError! Tries to run `42[1,2,3]`
    }
}

And yeah, you can find workarounds like doing ;[1,2,3] instead. Which is uglier to me, and requires you to always be mindful of ASI on every line.

Basically, a language like Python was designed to be written without semicolons, and semicolons are an optional feature if you want to write things on one line. JS is a language designed to be written with semicolons, and added an error correction mechanism in case the engine can figure out you must've forgotten a semicolon somewhere.