I wonder what this tag in script stands for <!--> //-->
I've noticed it in some usage of javascript, but not always.
Is this necessary or can you just keep it out and what is it's major use?

- 372,613
- 87
- 782
- 758

- 7
- 1
- 3
-
It's the HTML comment tag, and it was used in the past to avoid js code being displayed by browsers that didn't understand javascript. You can safely remove it nowadays. – bfavaretto Sep 10 '13 at 16:52
3 Answers
Before javascript existed, browsers did not know what <script>
meant. The tag didn't exist yet. So those browsers would attempt to output the javascript directly! To work around that, we would comment out the javascript with <!--
html comments -->
. However, the ending html comment -->
is a javascript syntax error, so we have to comment that out with //
javascript comments. Nowadays, browsers are smarter and can handle these edge cases.

- 26,951
- 10
- 71
- 101
Those are to create comments in XML and HTML.
<!-- this will not show -->
But this will.
They are included in scripts to hide the code from browsers (technically, this can be a number of different devices, but I'm using "browser" for short) which don't allow for JavaScript.
<script><!--
doSomethingJavaScripty();
// -->
</script>
This means the browser without JavaScript will not accidentally display doSomethingJavaScripty();
. It is often paired with a noscript
tag (which a browser that supports JS will safely ignore).
This is technically optional. Often times it is a relic of a now-forgotten past, but it is conceivable that it might be useful at some point. It might happen, for example, that you eventually want a service to read and parse your webpage. In such a case, adherence to good practice will prove beneficial. But, even in that circumstance, it has become more common (thankfully) to move to something called "unobtrusive JavaScript."

- 79,954
- 26
- 128
- 166
its used when developers wants to be on safe side, that if browser doesnot support javascript then the code will not be rendered as text, it will become comment

- 4,667
- 3
- 27
- 58