What is the best way to declare a script tag in HTML5?

I have seen many different ways to declare a script tag. Some of them:

Option 1:

 <script type="text/javascript">
     //some javascript here
 </script>

Option 2:

<script type="text/javascript">
     //<![CDATA[
     // some javascript here
     //]]>
</script>

Option 3:

 <script language="javascript">
     //some javascript here
 </script>

Option 4:

       <script>
          //some javascript here
       </script>

There are other options. When we work in HTML5 + js, then which method is more appropriate when declaring a script tag?

+4
source share
4 answers

In HTML5, you don’t need an additional type attribute. Just declare your JavaScript code with <script></script>.

+4
source

The default language for tags scriptis Javascript, so there is no need to explicitly declare it.

<script>
//some javascript here
</script>

Javascript. , .

MDN:

type: , script JavaScript

language: type, . type . .

3 , language MDN .

, <![CDATA[]]> , XML.

+2
Version 2 : format

javascript W3C.

It also allows the browser to identify that JavaScript or text written inside the tag

See this link for details:

Javascript and XHTML

0
source

JavaScript is the default scripting language for HTML5, so you can simply use the script tag without a type attribute. However, with HTML5 you get additional features like async.

0
source

All Articles