Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to include JavaScript in HTML

In this blog post, we are going to learn how to include JavaScript in HTML. If you are new to programming or just starting to learn JavaScript, this post is for you! We will go through the process step by step, providing actual code examples and explaining any jargon or technical terms that we come across. We will also use analogies and intuitions to help you understand the concepts better.

What is JavaScript?

Before we dive into how to include JavaScript in HTML, let's briefly explain what JavaScript is. JavaScript is a programming language that allows you to add interactivity and dynamic content to your websites. It is the language that powers everything from simple animations to complex web applications. In a nutshell, if you want your website to do more than just display static text and images, you need JavaScript.

Including JavaScript in HTML

Now that we know what JavaScript is, let's see how we can include it in an HTML file. There are two main ways to include JavaScript in HTML:

  1. Inline JavaScript: This is when you write your JavaScript code directly inside the HTML file, usually within a <script> tag.
  2. External JavaScript: This is when you write your JavaScript code in a separate file and then link to that file in your HTML file using the <script> tag.

Let's explore both of these methods in detail.

Inline JavaScript

Inline JavaScript is the simplest way to include JavaScript in HTML, as you don't need to create any additional files. To include inline JavaScript, you simply need to add a <script> tag to your HTML file and write your JavaScript code inside it. The <script> tag can be placed anywhere within the <head> or <body> of your HTML document.

Here's an example of how to include inline JavaScript in an HTML file:

<!DOCTYPE html>
<html>
<head>
    <title>Inline JavaScript Example</title>
</head>
<body>
    <h1>Click the button below to display an alert:</h1>
    <button onclick="displayAlert()">Click me!</button>

    <script>
        function displayAlert() {
            alert('Hello, World!');
        }
    </script>
</body>
</html>

In this example, we have a simple HTML file with a button. When the button is clicked, it calls the displayAlert() JavaScript function, which displays an alert with the message "Hello, World!".

Notice that the JavaScript code is placed inside the <script> tag, which is located at the end of the <body> section. This is a common practice because it ensures that the JavaScript code is executed after the HTML elements are loaded, preventing any errors or unexpected behavior.

External JavaScript

While inline JavaScript is easy to implement, it can quickly become difficult to manage as your codebase grows. This is where external JavaScript files come in handy. By placing your JavaScript code in a separate file, you can keep your HTML file clean and organized, making it easier to maintain and debug.

To include an external JavaScript file in your HTML file, you need to create a new file with a .js extension (e.g., script.js). Then, write your JavaScript code in this file, without the <script> tags. Finally, add a <script> tag with the src attribute in your HTML file, pointing to the external JavaScript file.

Here's an example of how to include an external JavaScript file in an HTML file:

index.html:

<!DOCTYPE html>
<html>
<head>
    <title>External JavaScript Example</title>
</head>
<body>
    <h1>Click the button below to display an alert:</h1>
    <button onclick="displayAlert()">Click me!</button>

    <script src="script.js"></script>
</body>
</html>

script.js:

function displayAlert() {
    alert('Hello, World!');
}

In this example, we have the same HTML file as before, but instead of including the JavaScript code directly within the <script> tag, we have created a separate script.js file. The <script> tag in the HTML file now has the src attribute, which points to the script.js file.

This method of including JavaScript allows you to keep your code organized and makes it easier to manage larger projects.

Best Practices

Now that we know how to include JavaScript in HTML, let's go over some best practices to follow when working with JavaScript and HTML.

Place your <script> tags at the end of the <body> section: This ensures that your JavaScript code is executed after all the HTML elements have been loaded, preventing any errors or unexpected behavior.

Use external JavaScript files: While inline JavaScript can be useful for small projects or quick tests, it is generally best to use external JavaScript files to keep your code organized and maintainable.

Include only one JavaScript file per page: While you can include multiple JavaScript files in a single HTML file, doing so can slow down your page load times and make your code harder to manage. Instead, try to organize your JavaScript code into a single file, or use a tool like a module bundler to combine multiple files into one.

Comment your code: Adding comments to your JavaScript code is a great way to document your code and make it easier for others (or yourself) to understand your code. Use comments to explain the purpose of your code, any assumptions it makes, or any quirks it has.

Test your code: Always test your JavaScript code on multiple browsers and devices to ensure that it works correctly and provides a consistent user experience.

Conclusion

In this blog post, we learned how to include JavaScript in HTML using both inline and external methods. We also discussed some best practices to follow when working with JavaScript and HTML.

By following these best practices and understanding how to include JavaScript in your HTML files, you'll be well on your way to creating dynamic and interactive websites that engage and delight your users. Happy coding!