<!— in Javascript [duplicate]

Tag: javascript , comments Author: honghui808 Date: 2013-09-25

This question already has an answer here:

I'm looking at a website using Firebug and noticed the following code:

<script language="javascript" type="text/javascript">
some code
];// -->

To me, < ! - - (without the spaces) is used in HTML to comment. So, everything between < ! - - and - - >
is ignored.

Is it different in JS? I can't managed to find a documentation explaining what those means. To me, comments in JS are // or /* ... */

Thank you

It came about when browsers couldn't understand certain newfangled tags. The convention was to wrap the jingly stuff in HTML comment tags in case the browser went ahead and rendered the content without understanding what it meant. The convention has always been for the browser to render any content inside tags it didn't understand.

Best Answer

The comments were a legacy approach to write javascript in the times whrere not all browsers supported it. This ensured that the javascript code will not appear as plain text on those browsers.

A common variant nowadays is also this:

<script language="javascript" type="text/javascript">
    some code

The <![CDATA[ appoach has different origins. Usually, when the site displays dynamic content comming from another markup file (XML in most cases), then non-markup code as JavaScript has to be "escaped".

Other Answer1

Some template systems use the synthax <!-- code -->, so the template engine can recognize it. If you are working with a framework or external libraries, take a look at that posibility.