console.log("woof")
(function() { console.log("foo") })()
Without semi-colons, that blows up (with a super unhelpful error message, to boot). Sure, if you understand ASI in JS, then you'll figure out the problem very quickly. But if you just always use semi-colons, then ASI is one fewer thing to have to worry about. console.log("woof")
void function() { console.log("foo") }()
There really is only one ASI rule you need to remember [0] in practice: don't begin a new line with ( or [ if it's intended to be a new statement.On this topic, it could so easily have been different; I found this interesting from Brendan Eich [1]:
> I wish I had made newlines more significant in JS back in those ten days in May, 1995. Then instead of ASI, we would be cursing the need to use infix operators at the ends of continued lines, or perhaps or brute-force parentheses, to force continuation onto a successive line. But that ship sailed almost 17 years ago.
> ...My two cents: be careful not to use ASI as if it gave JS significant newlines...
[0] this is technically a lie because everybody needs learn the other one whether they use semicolons or not: don't put a linebreak after the "return" keyword :) [1] https://brendaneich.com/2012/04/the-infernal-semicolon/
Here's an even easier and clearer one: finish every statement with a semicolon.
function hello() {
var x = 0;
return
{
x: x,
y: y
};
};
Just putting semicolons where you think it can work won't help you. If you write javascript, you must know what the asi does. So semicolons or no semicolons is completely irrelevant.