Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is <!-- a valid comment delimiter in JavaScript

Tags:

javascript

Is <!-- a valid comment delimiter in JavaScript?

var foo = 'foo'; <!-- Is this a valid single-line comment?
like image 219
Ben Aston Avatar asked Jun 22 '26 07:06

Ben Aston


1 Answers

Yes, it apparently is.

This syntax was included to cater for browsers that do not support JavaScript, to avoid them rendering the code inside <script> tags. This was more important in the early days of the Web, when a large proportion of browsers did not support JavaScript.

Both <!-- and --> delimit single line comments.

<!-- this is treated as a single line comment
--> and this is treated as a single line comment

So you can write your script tags using the following syntax:

<script>
<!--
console.log('this is my code')
-->
</script>

Note that the Wikipedia page on JavaScript syntax does not mention this.

like image 98
Thilo Avatar answered Jun 23 '26 20:06

Thilo