Can you combine JavaScript comment syntax? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

20th Jan 2024, 12:17 AM
Chloe Robinson
Chloe Robinson - avatar
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.
20th Jan 2024, 2:10 AM
Bob_Li
Bob_Li - avatar
+ 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 /////////////*/
20th Jan 2024, 5:16 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Tibor Santa , Nice trick. You only need to modify one end.
22nd Jan 2024, 9:10 PM
Rain
Rain - avatar
+ 2
//////////////////////////// ///code blings 😁/// ////////////////////////////
20th Jan 2024, 5:18 AM
Bob_Li
Bob_Li - avatar
+ 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.
20th Jan 2024, 6:00 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Tibor Santa nice. // /* console.log('Uncomment the single line comment above to disable this log message.'); //*/ not confusing at all...🤔
20th Jan 2024, 6:01 AM
Bob_Li
Bob_Li - avatar
+ 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>
22nd Jan 2024, 9:02 PM
Rain
Rain - avatar