Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript automatic semicolon insertion for do-while statements

Since ES6, a new case has been added for rule 1 of automatic semicolon insertion:

The previous token is ) and the inserted semicolon would then be parsed as the terminating semicolon of a do-while statement (13.7.2).

This allows one to avoid line termination and write ugly code such as:

do {} while (false) var a = 42

What is the rationale behind this rule? Some useful use-case?

like image 753
lledr Avatar asked Mar 09 '26 04:03

lledr


1 Answers

I'm pretty sure that "case" added in ES2015 is only there to standardize rules which browsers had already implemented in order to be compatible with terribly-written (or weirdly minified) scripts. It wasn't exactly a new feature, so much as it was a tweak of the specification to be in line with what browsers were doing already.

For example, your snippet runs in IE11, which was released in 2013:

do {} while (false) var a = 42;
console.log('no parse errors');
like image 101
CertainPerformance Avatar answered Mar 10 '26 18:03

CertainPerformance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!