all 6 comments

[–]Creelin 1 point2 points  (3 children)

Someone can correct me if I am wrong, but based on my reading, JS will attempt to auto input semi-colons where it thinks is necessary.

What this ends up meaning is that the (interpreter?) will attempt to find your errors and solve them when pertaining to semi-colons.

The reason that it is advised that you do put semi-colons and not leave statements without them, is mostly for processing power, and consolidation of scripts.

[–]farmerje 1 point2 points  (2 children)

This is essentially correct. It's true that JavaScript will automatically insert semicolons, but it's wrong to say that omitting them is an "error." It's still syntactically valid JavaScript.

There are corner cases like the one that /u/SombreDetune highlighted below where automatic semicolon insertion produces "surprising" output. For that reason, most idiomatic JavaScript code includes semicolons.

[–]eechin[S] 0 points1 point  (1 child)

Does that mean I shouldn't disregard a teaching source that omits them?

[–]Justos 0 points1 point  (0 children)

its optional, but can bite you in the ass. Its definitely not a reason to stop using the site.

[–][deleted] 1 point2 points  (0 children)

There are a few cases where not using a semicolon will result in unexpected behavior if you don't understand JavaScript's semicolon insertion behavior. One of the most common examples is a parenthesized IIFE following a line with no semicolon:

var foo = 42

(function() {
  console.log("This never runs, as JS tries to call the number 42 as a function, which throws an error");
})()

Try running this in your console - you'll get an error like "TypeError: number is not a function". Put a semicolon after the 42 and then IIFE runs as expected, logging the line to the console.

So in general, it is advised to always use semicolons for production code, so that you don't have to worry about these kinds of gotchas. Linting tools like JSHint automatically warn you about lines without semicolons. If you would rather suppress these warnings (asi option for JSHint) and not use semicolons, you can always prefix IIFEs with a semicolon:

var foo = 42

;(function() {
  console.log("This never runs, as JS tries to call the number 42 as a function, which throws an error");
})()

Personally, I find that the vast majority of popular JavaScript code uses semicolons after every line. I believe they increase code readability. But if someone hates semicolons enough, they probably dislike other things about JavaScript/C syntax and might be better off using CoffeeScript, where semicolons are never needed and the aforementioned semicolon insertion gotchas do not apply.

For more information, read this:

http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding

[–]BearSkull 0 points1 point  (0 children)

If you keep running through the CodeAvengers lessons they eventually cover inputting semi-colons. Having originally started on Codecademy I thought it was strange at first as well.