In this blog, you will learn to add JavaScript in HTML. JavaScript is one of the scripting languages that is used in web development to create modern and interactive web pages. There is a lot of difference between JavaScript and java so don’t consider them one language.
JavaScript is also known as JS. JavaScript programs cannot be executed on their own. They are always executed with the help of HTML code.
There are three ways to add JavaScript in HTML:-
- Embedding code
- Inline Code
- External File
![JavaScript](https://codeblockx.com/wp-content/uploads/2023/01/codeblockx-24-768x432.png)
Let’s Discuss each Way to add JavaScript in HTML one by one:-
- Embedding Code:- To add this type of JavaScript in HTML, we use the <script> tag inside the HTML file. We can add this tag in the <body> as well as in the <head> tag usually depending on the structure of the website. The code and output for both types are:-
Index.html
OUTPUT
![](https://codeblockx.com/wp-content/uploads/2023/01/Screenshot_20230107_123915.png)
Index1.html
OUTPUT
![Types of JavaScipt](https://codeblockx.com/wp-content/uploads/2023/01/Screenshot_20230107_124103.png)
2. Inline code:- This type of code of JavaScript is used when we have to use call functions in the HTML event attribute. The code and output of this type will be:-
Index2.html
OUTPUT
![Inline JavaScript](https://codeblockx.com/wp-content/uploads/2023/01/Screenshot_20230107_125556-768x417.png)
3. External File:- This type of JavaScript is used to make a different file for JavaScript with the extension .js. This file is added to the HTML file with the help of the src attribute under the script tag. This file is very helpful as it helps to reuse the code and helps in the easy maintenance of the website. The code for this type of file will be:-
Index3.html
Script.js
OUTPUT
![External JavaScript](https://codeblockx.com/wp-content/uploads/2023/01/Screenshot_20230107_010509-768x375.png)
Hope this blog was helpful to you and you have got an answer to adding javaScript in HTML. If you want to learn about adding CSS in HTML. You can check out our detailed blog on it.
Thank you for visiting our blog. If you have doubts related to any coding questions then let us know in the comments section. We will answer them as soon as possible.