Skip to content


JavaScript Block Comments

I ran across an interesting little tidbit in a JavaScript book, I think, or online. Likely I saw it in something Douglas Crockford wrote, but couldn’t be certain. Anyway, it has to do with comments in JavaScript. The language supports two comment types:

1. Single line using the double slash:

// This is a comment.

2. Multiline (also called block) using the slash-asterisk combination:

/* This comment
can go over multiple
lines. */

The argument made (wherever it was made) is that you shouldn’t use the multiline comment style as the */ character combination can appear in JavaScript code. Namely, it might appear in a regular expression match, when slashes are used to delineate the pattern and the asterisk is a pattern modifier that looks for zero or more of something. A syntax error will arise if you use block comments around some code that includes a problematic regular expression:

/* some code
var x = 'some string'.search(/[a-z]*/g);
some code */

That’s obviously a useless example in itself, but it shows a situation in which a problem is caused by using block comments.

All that being said, I wouldn’t suggest that you avoid using multiline comments. They’re quite handy, both for adding lots of documentation and for debugging purposes (by making blocks of code inert). But the potential conflict is something to be aware of, particularly when you apply multiline comments to render some code non-executing and all of a sudden have a syntax problem.

Posted in JavaScript.


6 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Jason says

    Must have been old advice surely? Just checked JavaScript the Definitive Guide (fifth edition) from O’reilly by David Flanagan and there was no discouraging against using multiline comments (nor was there in the other two books). Most people should/would/could use an editor with syntax highlighting, that would easily spot any issues.

    • Larry says

      Thanks for the response (and for continuing to check out the blog). Actually, I’m almost positive I read this in Douglas Crockford’s newest book, JavaScript: The Good Parts. But regardless, you could end up with syntax errors using block comments, so it’s not really a new/old thing. Still, I agree that I’m not going to stop using them, but I thought it was an interesting thing to be aware of. And, of course, some programmers take every little possible problem quite seriously!

  2. Jason says

    Found it here: http://oreilly.com/catalog/9780596517748/preview
    “In JavaScript, those pairs can also occur in regular expression literals, so block comments are not safe for commenting out blocks of code.”

    Have to admit, that is the most ridiculous paragraph I have read in a Programming book so far.

    • Larry says

      Hey! Thanks for confirming the source. I still have half a brain left in me. It’s hard to argue with Crockford when it comes to JavaScript, but I agree that “not safe” is a stretch here. Thanks again for following up on this!

  3. Floydian says

    What really gets me is that CSS doesn’t allow for single line comments using the // method. It’s nice that CSS has the multi line comment, but sometimes it’s nice to be able to do just that one line…

    But I digress, Crockford also recommends against using ++ to increment numbers in javascript. He’s definitely a cautious programmer.

    • Larry says

      Thanks for the comments. What’s Crockford’s argument against ++? Is it a matter of pre- and post- and precedence?

If you need quick assistance with a question or problem related to one of my books, please use the support forums instead.

Some HTML is OK

or, reply to this post via trackback.