How to add JavaScript to HTML?

Posted by

JavaScript Versions: How JavaScript has changed over the years

JavaScript is a light weight, interpreted and easy to lean programming language. JavaScript is an open source and run on browser, it is designed for creating network centric application.

JavaScript is very easy to implement because it is integrated with HTLM.

It is first discovered by “Brenden Eich” in 1995, JavaScript is also known as ECMAScript. Currently the update version is ECMA6. In other words, we can say that the term scripting is used for languages that require the support of another language to get executed. For example, JavaScript programs cannot get executed without the help of HTML or without integrated into HTML code.

JavaScript is used in several ways in web pages such as generate warning messages, build image galleries, DOM manipulation, form validation, and more.

Adding JavaScript to HTML Pages

There are possibly 3 ways in which a user’s can add JavaScript to HTML pages. These steps are:

  • Embedding code
  • Inline code
  • External file

We will see three of them step by step

II. Embedding code:-

To add the JavaScript code into the HTML pages, we can use the <script>…..</script> tag of the HTML that wrap around JavaScript code inside the HTML program. Users can also define JavaScript code in the <body> tag (or we can say body section) or <head> tag because it completely depends on the structure of the web page that the users use. We can understand this more clearly with the help of an example, how to add JavaScript to html.


This is not it, we can also define the JavaScript code in the <body> tags or body section.

Let’s understand through an example.


II. Inline code:-

Generally, this method is used when we have to call a function in the HTML event attributes. There are many cases (or events) in which we have to add JavaScript code directly eg. OnMover event, OnClick, etc. Let’s see with the help of an example, how we can add JavaScript directly in the html without using the <script>…. </script> tag.

Let’s look at the example:-

III. External file:-

We can also create a separate file to hold the code of JavaScript with the (.js) extension and later incorporate/include it into our HTML document using the src attribute of the <script> tag. It becomes very helpful if we want to use the same code in multiple HTML documents. It also saves us from the task of writing the same code over and over again and makes it easier to maintain web pages.

In this example, we will see how we can include an external JavaScript file in an HTML document.

Let’s understand through a simple example:-

Function code written in external .js file which we connect with html files.

Code in .js file is

function display() { 

alert(“Hello JavaScript!”); 

Wrapped up

Both of the above programs are saved in the same folder, but you can also store JavaScript code in a separate folder, all just you need to provide the address/path of the (.js) file in the src attribute of <script> tag. External JavaScript file only contains JavaScript code and nothing else, even the <script>…. </script>tag are also not used in it. I hope you like this particular information about addition of JavaScript file helpful.

Keep practising and Exploring.

Thank You!!