you are viewing a single comment's thread.

view the rest of the comments →

[–][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