What is Internal and External JavaScript?
Last Updated :
24 May, 2025
JavaScript is a popular programming language used to add interactivity to web pages—like showing alerts, handling button clicks, or updating content without refreshing the page.
There are two main ways to add JavaScript to an HTML file:
- Internal JavaScript: Written directly inside the HTML file using the <script> tag.
- External JavaScript: Written in a separate .js file and linked to the HTML file using the src attribute of the <script> tag.
Internal JavaScript
Internal JavaScript refers to embedding JavaScript code directly within the HTML file using <script> tag, either inside the <head> or <body> tag. This method is useful for small scripts specific to a single page.
Syntax
<script>
// JavaScript code here
</script>
HTML
<html>
<body>
<script>
/*Internal Javascript*/
console.log("Hi Geeks, Welcome to GfG");
</script>
</body>
</html>
Output
Hi Geeks, Welcome to GfG
In this example
- <script> is used to write internal JavaScript directly inside the HTML file.
- console.log("Hi Geeks, Welcome to GfG"); prints the message to the browser’s console, not on the web page.
- The comment /* Internal JavaScript */ is for reference and does not affect the code.
Advantages
- Simple to implement: No need for additional files; the script is written directly in the HTML.
- Quick to test: Easy to add and modify scripts for small tasks.
- Single file: Everything is contained in one file (HTML and JavaScript).
Disadvantages
- Limited reusability: The script is tied to the specific HTML file, so it can’t be reused across multiple pages.
- Harder to maintain: As the project grows, keeping the JavaScript in the HTML can make the code harder to manage.
- Larger HTML files: The HTML file can become large and harder to read with more complex JavaScript.
External JavaScript
External JavaScript refers to writing your JavaScript code in a separate file with a .js extension and then linking it to your HTML file. This method helps you keep your HTML clean and your JavaScript code organized.
Syntax
<script src="url_of_js_file"> </script>
Multiple Script files can also be added to one page using several <script> tags.
<script src="file1.js"> </script>
<script src="file2.js"> >/script>
HTML
<html>
<head>
<title>External JavaScript Example</title>
<script src="script.js"></script>
</head>
<body>
<button onclick="showMessage()">Click Me</button>
</body>
</html>
JavaScript
// Filename: script.js
let h2 = document.getElementById("demo");
h2.innerText = "This text is added by External JavaScript";
Output

In this example
- This HTML file contains a <button> element. When clicked, it calls the showMessage() function defined in the external JavaScript file (script.js).
- document.getElementById("demo"): Finds the HTML element with the ID demo. However, in this code, there's no element with the id="demo", so this part of the code will cause an error unless the element is added to the HTML.
- h2.innerText = "This text is added by External JavaScript";: Changes the inner text of the element to display the message.
Advantages
- Reusability: The same JavaScript file can be linked to multiple HTML pages, improving code reuse.
- Better organization: Separates HTML structure from JavaScript, keeping the code cleaner and more maintainable.
- Faster load times: Browsers cache external JavaScript files, making subsequent page loads faster.
Disadvantages
- Additional HTTP request: Each external file requires an extra request to the server, which can slightly increase load time for the first visit.
- File path issues: If the file path is incorrect, the script won’t load, leading to errors.
- Dependency on external file: If the external file is missing or fails to load, the JavaScript won’t work.
Difference Between Internal JavaScript vs External JavaScript
Below are the some key differences between internal JavaScript and external JavaScript:
Internal JavaScript | External JavaScript |
---|
Written directly within the HTML file using <script> tag. | Written in a separate .js file and linked to the HTML file using <script src="filename.js"></script>. |
Inside the HTML file (within <script> tags). | In a separate .js file, referenced in HTML. |
Not reusable; specific to the HTML file. | Reusable across multiple HTML pages by linking the same .js file. |
Can make the HTML file large and hard to manage with bigger scripts. | Keeps the HTML clean and code organized in a separate file. |
Difficult to maintain for larger projects. | Easier to maintain and debug as code is separated. |
Slower loading, as the script is loaded each time the page is loaded. | Faster page load times due to browser caching of external files. |
Good for small scripts or one-time tasks. | Ideal for large scripts or code shared across multiple pages. |
Usually placed in the <head> or <body> section of the HTML. | Placed at the end of the <body> or in the <head> section of HTML. |
Quick scripts or simple tasks specific to one page. | Complex scripts, functions used across multiple web pages. |
No file extension; directly written within HTML. | Must have a .js file extension (e.g., script.js). |
Conclusion
In this article we'll covered Internal JavaScript, which is written directly inside the HTML file and is good for small, page-specific tasks, and External JavaScript, which is kept in a separate file, making the code cleaner, reusable, and easier to maintain. While internal JavaScript is simpler for small scripts, external JavaScript is preferred for larger projects due to its reusability and organization.