Are comments literally just stripped out of your source before parsing, or can they count as linebreaks and disrupt continuity in certain contexts?
'foo'.replace(/f/, 'b') //f->b
.replace(/o/, 'a') //o->a
.replace(/o/, 'r') /*o->r*/ ;
'foo'.replace(/x/, /*matches "x"*/ 'y');
var foo = ( true !== false ) ? // bikeshed
'bar' : /*if they're equal, which they won't be, we'll want 'baz'*/ 'baz';
You know, cause they say whitespace is "safe" and "insignificant" or whatever, but we all know there are exceptions to that. Are comments actually safe?
They're ignored during parsing, if they were stripped out before parsing, the parser would need to scan the input twice.
However, the LineTerminator at the end of the line is not considered to be part of the single-line comment; it is recognised separately by the lexical grammar and becomes part of the stream of input elements for the syntactic grammar. This point is very important, because it implies that the presence or absence of single-line comments does not affect the process of automatic semicolon insertion
ES5 Specification for comments.
The source code is tokenised as if the comments didn't exist.
Yes, comments are safe.
(That being said, I've seen some broken server-side HTML minifiers that don't know what inline JavaScript is, and removes all of the line breaks. A comment beginning with //
comments out the entire script.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With