Just a Semicolon

Chris Coyier Chris Coyier on

A silly debate that rages just as hard as “tabs vs spaces” is “semicolons or not” in JavaScript. Generally, the answer is “use an automatic formatting tool so you don’t have to think about it”. But if you happen to be on the “no semicolons” side, it’s interesting to note that it can cause confusing bugs sometimes. Thomas Steiner found a fun one:

console.log()
(function(){})Code language: JavaScript (javascript)

Just a little log statement followed by an IIFE, right? Click over to see what actually happens.

It's time to take your JavaScript to the next level

Frontend Masters logo

Frontend Masters is the best place on the web to really learn JavaScript. We have a complete learning path from the biggest and best teachers in JavaScript to help you make the most out of the web's biggest language.

7-Day Free Trial

One response to “Just a Semicolon”

  1. toastal says:

    If you do return & then a newline with your return value, you also get an issue of returning nothing. You need to be aware of ASI’s rules regardless—which is why IIFEs have prefixed with ;(function… for the longest time as an ad-hoc ‘standard’. Once you know the rules, you may as well omit those extra characters except where needed, like here.

Leave a Reply to toastal Cancel reply

Your email address will not be published. Required fields are marked *

$839,000

Frontend Masters donates to open source projects through thanks.dev and Open Collective, as well as donates to non-profits like The Last Mile, Annie Canons, and Vets Who Code.