Comment tags in JavaScript


i have a task to clean up javascript / html comments from jsp code, preferably via regex.
I found something like this:

<script language="JavaScript">
                // -->

And i'm unsure what this actually is. Is this something for old IE versions?

Thanks for you help
Fabian MaassAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

meb_santosoConnect With a Mentor Commented:
HTML comments, ie. <!-- -->, are no longer needed. They were intended to allow (very old) browsers that didn't understand the <script> tag to degrade gracefully (see it as a fallback scenario).

These (very) old browsers are just like dinos, we practically can't find it anywhere, so frankly it's pointless to use the <!-- -->.
Do note that IF you explicitly still want to use comments before and after the javascript function, place double slashes before the closing comment tag:


This prevents JavaScript from executing the --> tag.

...or use commented out CDATA tag:

<script type="text/javascript">
document.write("Hello World!");

Can you delete those <!-- --> lines? Go ahead, unless there are actual comments in there that is needed for debugging purposes.
John EastonConnect With a Mentor DirectorCommented:
As far as I know this is to support older browsers - i.e. one's which do not support the <script> tags.  Unless you are supporting very old clients it shouldn't be needed.
Terry WoodsIT GuruCommented:
If you're wanting help with the regex, you're probably best to ask that in a separate question. There is a "Regular Expressions" topic area that is well attended.
Fabian MaassAuthor Commented:
thank you all for your feedback.

so, to summarise:

1. using <!-- ... --> inside a javascript function was intended for old browsers, yet it is NOT a comment but the commands inside are evaluated
2. valid comments inside a javascript function should use the CDATA tag
3. the correct way to use html comments inside a javascript function would be <!-- //--> as the greater than sign will be evaluated otherwise

i think there is a misunderstanding on my part regarding the double slash. The answers say "HTML comments, ie. <!-- -->, are no longer needed.", yet i was not asking for tags " --> " but for " // --> ".

Please confirm my summary ;)

Thank you all
Summary 1,2,3 are accurate.

Regarding the "// -->" : it's a mixed up world, between "I want to write (HTML) comments with the <script> </script> so I can humbrag about my functions that I copy-pasted from some other script" and "No, javascript, please ignore the dash-dash-greaterThan, just leave it alone".

It was a good practice back in the days where you want to do the above to think of a fallback scenario for the older browsers, but as JEaston also wrote, it's no longer needed.
All Courses

From novice to tech pro — start learning today.