I recently wrote a parser/analyzer for JavaScript, where I had to painstakingly implement ASI, and I also have my copy of Crockford's JavaScript: The Good Parts in front of me hereon my bookshelf, where itwhich advocates alwaysalways using semicolons. His intentions were The intention was good when recommending this, but unfortunately his recommendation (to always use them)it doesn't always help in practice.
Obviously, the people writing frameworks like jQuery, zepto, etc. are JavaScript syntax masters and thus they know the difference between:
return { status: true }; and
return { status: true }; JavaScript, while powerful, is also a beginner's languagebeginner's language and good luck explaining this to someone who is just learning what a for loop is. Like introducing most people to a new skill, there are some more complex things you don't want to explain right away, so instead you choose to instill a "cargo cult" belief in certain things just to get them off the ground. So, you have two choices when teaching a beginner how to write JavaScript:
- Tell them "follow this one rule and don't ask why", telling them to put a always semicolon at the end of every line. Unfortunately, this doesn't help in the example in the above, or any other example where ASI gets in the way. And Mr. or Ms. beginner programmer gets befuddled when the code above fails.
- Tell them "follow these two rules and don't ask why", telling them not to bother with semicolons everat the end of every line, and to instead always a) Follow
returnwith a{, and b) When a line starts with a(, prepend it with a;.
Choosing option 2 is a better set of "cargo cult" rules to follow (will result in very few ASI-related bugs), and in every other caseeven if you do get a deep understanding of the topic, you have fewer unneeded characters on the screen.