How to js file in html

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: To include a JavaScript file in your HTML, you use the `<script>` tag. Place this tag within the `<head>` or `<body>` section of your HTML document, specifying the `src` attribute with the path to your `.js` file.

Key Facts

What is a JavaScript File?

A JavaScript file, typically with a `.js` extension, is a plain text file containing JavaScript code. This code can be used to add dynamic behavior, interactivity, and complex features to web pages, such as animations, form validation, and updating content without reloading the page.

How to Include a JavaScript File in HTML

There are two primary ways to include JavaScript in an HTML document: embedding it directly within the HTML file using the <script> tag, or linking to an external `.js` file using the same tag with the src attribute.

Using an External JavaScript File (Recommended)

This is the most common and recommended method for several reasons:

To link an external JavaScript file, you use the <script> tag with the src attribute:

<!DOCTYPE html><html><head><title>My Web Page</title><!-- Link to an external JavaScript file --><script src="path/to/your/script.js"></script></head><body><h1>Hello, World!</h1></body></html>

In this example, path/to/your/script.js should be replaced with the actual relative or absolute path to your JavaScript file. If the file is in the same directory as your HTML file, you would simply use script.js.

Where to Place the <script> Tag

The placement of the <script> tag can affect the loading and rendering performance of your web page.

1. In the <head> Section:

Placing the script in the <head> section means the browser will fetch and execute the JavaScript file before it starts rendering the HTML content of the page. If your JavaScript is large or takes a long time to execute, this can lead to a noticeable delay before users see any content on the page, resulting in a poor user experience.

<head><title>Page Title</title><script src="scripts/my-header-script.js"></script></head>

2. Just Before the Closing </body> Tag (Recommended for Performance):

Placing the script just before the closing </body> tag is generally the preferred method for performance. This allows the browser to parse and render the HTML content first, making the page appear to load faster for the user. The JavaScript will then be fetched and executed after the DOM (Document Object Model) is ready.

<body><!-- Your HTML content here --><p>Welcome to my page!</p><!-- Link to an external JavaScript file --><script src="scripts/my-footer-script.js"></script></body>

Using the async and defer Attributes

Modern HTML5 provides two attributes for the <script> tag that offer more control over how external scripts are loaded and executed:

<script async src="scripts/analytics.js"></script>
<script defer src="scripts/main-logic.js"></script>

Embedding JavaScript Directly (Less Common for Larger Scripts)

You can also write JavaScript code directly within the <script> tags in your HTML. This is suitable for very small snippets of code or for testing purposes.

<!DOCTYPE html><html><head><title>Inline Script Example</title></head><body><h1>Check the console!</h1><script>// Inline JavaScript codeconsole.log("Hello from inline JavaScript!");alert("This is an alert from inline script.");</script></body></html>

Again, placing inline scripts just before the closing </body> tag is generally better for performance.

Summary

Including JavaScript files in your HTML is a fundamental aspect of web development. Using external `.js` files linked via the <script src="..."> tag is the standard and most efficient practice. For optimal performance, consider placing these script tags just before the closing </body> tag, or utilize the defer attribute for scripts that rely on the DOM. The async attribute is best for independent scripts that can run without affecting page rendering order.

Sources

  1. MDN Web Docs - Script: The Script Embed: Create and control embedded contentCC-BY-SA-2.5
  2. W3Schools - HTML JavaScriptCC BY-NC-SA 4.0
  3. How To Include JavaScript In HTML: The &lt;script&gt; Tag - freeCodeCampfair-use

Missing an answer?

Suggest a question and we'll generate an answer for it.