How to Add JavaScript in HTML code?

We can add javascript in HTML page by using a </script> tag. Before some time script tag attribute type=”text/javascript” was mandatory but now in new version HTML5, it is not required.

The general syntax of JavaScript is:

Note:- If you are working on HTML5, then type=”text/javascript” attribute not require in script tag.

We have different places where we can add javascript in the HTML page

  • In head tag
  • In body tag
  • In both head and body at a time

How to insert JavaScript external files in HTML?

If you want to use the same script or program in several pages, then we will create an external file for JavaScript and save it with filename.js extension. For executing the script we can insert it with HTML pages in the head tag or body tag where you want.

Syntax is:

The syntax is different for insert external JavaScript file, use an attribute in script tag as src=”filename.js”.

Note: In external JavaScript file we cannot use HTML script tag, there we can write script code with freedom.

Leave a Reply

Your email address will not be published. Required fields are marked *