0
Can you combine JavaScript comment syntax?
For instance, is this a valid comment? /*Outputs text// I know generally /**/ is used for multiline and // at the start for single line, but I was wondering if theoretically this could exist?
7 Answers
+ 3
it would be confusing.
Would the // be considered the end of /* or the start of a single line comment of the following line?
The current system is fine.
And it is commonly used by other languages, so I think there is no benefit in changing the syntax.
Multiline comments starts with /* and ends with */.
single line comments starts with // and ends when the line ends.
+ 3
Your example is an unfinished multiline comment, so it is invalid. It needs to end with */
It is legit to put // inside the multiline comment but it has no syntactic effect whatsoever. Sometimes people do it to format a comment nicely as if it were a box, but it is just unnecessary ceremony.
/*/////////////
// comment
// in a box
/////////////*/
+ 3
Tibor Santa ,
Nice trick. You only need to modify one end.
+ 2
////////////////////////////
///code blings đ///
////////////////////////////
+ 2
There is one situation I used something like this: when I wanted to occasionally disable a larger part of the code during development. Consider this:
///*
console.log('this code is active');
//*/
So right now the first and last lines are both single-line comments, so anything inbetween is active code that will run.
But if I remove // from the beginning, it becomes a valid multiline comment and any code between these two lines just becomes part of the comment and will not run.
There are some risks with overusing this pattern though, if there are already some multiline comments inside the code block, then it will break.
And modern IDE have convenient key combinations or commands to comment many lines at once, so using such tricks is really not necessary.
+ 2
Tibor Santa
nice.
// /*
console.log('Uncomment the single line comment above to disable this log message.');
//*/
not confusing at all...đ€
+ 2
Chloe Robinson ,
To me, the usefulness of combining comment styles is when commenting out a portion of commented code.
<code here> // <comment here>
<code here> // <comment here>
/*
<code here> // <comment here>
<code here> // <comment here>
*/
<code here> // <comment here>
<code here> // <comment here>