JavaScript is a powerful programming language that adds interactivity and dynamic functionality to HTML documents. Whether you want to create interactive forms, perform calculations, or manipulate HTML elements dynamically, JavaScript is essential for web development.
In this tutorial, we’ll explore How to Add JavaScript in HTML Document. We’ll learn about :
✅ Running JavaScript in an HTML file.
✅ Writing JavaScript inside an HTML document.
✅ Linking an external JavaScript file to an HTML document.
✅ Interview tips related to JavaScript integration.
By the end, you’ll have a solid understanding of how to use JavaScript efficiently in your web pages. So, Let’s get started –

How to Run JavaScript on an HTML File?
To run JavaScript in an HTML file, follow these simple steps:
- Create an HTML File :
- Use any text editor like VS Code, Sublime Text, or Notepad++.
- Save the file with a
.html
extension.
- Write JavaScript Inside the
<script>
Tag :- You can write JavaScript inside the HTML document within
<script>
tags.
- You can write JavaScript inside the HTML document within
- Open the File in a Browser
- Open the HTML file in Chrome, Firefox, or Edge to see JavaScript in action.
Let’s look at a basic example of running JavaScript inside an HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript in HTML - LogicalDuniya</title>
</head>
<body>
<h1>Welcome to JavaScript - LogicalDuniya</h1>
<script>
alert("Hello, this is JavaScript running inside an HTML file! -- LogicalDuniya");
</script>
</body>
</html>
Note :
If you see an alert pop-up when opening the HTML file, it means JavaScript is working correctly!
How is JavaScript Written in HTML?
JavaScript can be written in an HTML document in three ways:
1. Inline JavaScript (Inside HTML Tags)
JavaScript can be written inside HTML elements using the onclick
or onchange
attributes.
<button onclick="alert('Button clicked! - LogicalDuniya')">Click Me</button>
Note :
Best for small event-driven tasks, but not recommended for large scripts.
2. Internal JavaScript (Inside <script>
Tag in HTML)
JavaScript can be written inside the <script>
tag within the <head>
or <body>
section.
<!DOCTYPE html>
<html>
<head>
<script>
function showMessage() {
alert("This is an internal JavaScript function! - LogicalDuniya");
}
</script>
</head>
<body>
<button onclick="showMessage()">Click Me - LogicalDuniya</button>
</body>
</html>
Note :
Best for scripts that need to run only on that page.
3. External JavaScript (Using a Separate .js
File)
To keep your HTML clean and structured, it is best practice to write JavaScript in an external file and link it using <script src="filename.js">
.
Step 1: Create a JavaScript File
Create a file named script.js
and write the JavaScript code inside it:
function showMessage() {
alert("This message is from an external JavaScript file!");
}
Step 2: Link the JavaScript File in HTML
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<button onclick="showMessage()">Click Me</button>
</body>
</html>
Note :
Best for reusing scripts across multiple pages and maintaining cleaner code.
How to Link an External JavaScript File in HTML?
To link an external JavaScript file, follow these steps:
Step 1: Create a JavaScript file (script.js
).
Step 2: Write JavaScript code inside script.js
.
Step 3: Add the <script>
tag inside the HTML file’s <head>
or <body>
.
💡 Interview Tip:
In an interview, the interviewer might ask to you – How will you optimize the page load speed.
Then, you can answer – One way to improve the page load speed is by placing the <script>
tag before the closing </body>
tag.
<script src="script.js"></script>
</body>
</html>
This keeps HTML and JavaScript separate, making the code more readable and maintainable!
Interview Tips 🚀
We have seen a couple of important interview tips in above guide. Here are a few more super useful interview tips related to <script>
tag and usage of JS inside HTML –
1️⃣ What is the best way to include JavaScript in HTML?
- The best approach is to use an external
.js
file for scalability and maintainability.
2️⃣ Why should we avoid inline JavaScript?
- Inline JavaScript mixes logic with structure, making the code harder to maintain and debug.
3️⃣ Why place <script>
before </body>
?
- Placing scripts at the bottom ensures the HTML loads before JavaScript executes, improving performance.
4️⃣ Can we use multiple <script>
tags?
- Yes, multiple
<script>
tags can be used for different functionalities.
Final Notes
In this tutorial, we learned How to add JavaScript in an HTML document using three different methods: inline JavaScript, internal JavaScript, and external JavaScript.
✅ We started by understanding how JavaScript runs in an HTML file.
✅ Then, we explored different ways of writing JavaScript inside an HTML document.
✅ We also learned how to link an external JavaScript file using the <script>
tag.
✅ Finally, we discussed some useful interview tips related to JavaScript integration in HTML.
Using the right approach to add JavaScript in an HTML document ensures clean, efficient, and maintainable code. For small tasks, inline JavaScript may work, but for better scalability and reusability, external JavaScript files are recommended.
Next –
- To learn more simple and detailed JavaScript guides, do check our “Free” JavaScript Mastery Series.
- To use hundreds of “Free” tools to fasten your daily development and testing tasks, visit – Tools.LogicalDuniya.com
- To know about more tutorials of other latest technologies & concepts, visit our Tutorials page.
Happy Learning, Cheers..
Shubham : )