Loading...

JavaScript essentialsGetting Started

Your first script & adding JS to HTML

8 min lesson

Next
  • A script is a JavaScript file, and adding it to an HTML page means telling the browser to load and run that file.
  • You need this because most real JavaScript lives in a file connected to a web page, not typed line by line in the console.
  • Think of the HTML page as a house and the script as an electrician: the house is built first, then the electrician comes in and wires it up.
  • HTML is the language that describes the structure of a page, like headings and paragraphs.
  • The <script> tag is the HTML instruction that tells the browser to load a JavaScript file.
  • The src attribute is the part of the tag that gives the file's location, like src="app.js".
  • The basic form is: <script src="app.js"></script>, placed near the end of the body so the page content is ready first.
  • Real websites load dozens of scripts this way, and the browser runs them in the order they appear.
  • An easy-to-miss detail: if the file path is wrong, nothing happens and the console shows a 404 error.
  • The example page loads a script that prints a message to the console, and you will see that message in DevTools.
Keep in mind
  • Use a <script> tag with src to connect JavaScript to an HTML page.
  • Put the script near the end of the body so the page is ready.
  • Check the console for errors if the script seems to do nothing.
  • Getting the file path right is half the battle, and you will get faster at it with practice.
app.js
// This script runs when the page loads and prints a message.
// The browser runs it automatically because index.html loads it.

// Print a friendly message to the console.
console.log("The page has loaded and the script is running.");

// Print a second line to show the script ran to the end.
console.log("Hello from app.js!");

What happens: The page has loaded and the script is running. Hello from app.js!

Breaking it down

HTML file

<script src="app.js"></script>

This tag tells the browser to load and run app.js. Without it, the browser would never know the script exists.

Script file

app.js

This is a plain text file holding JavaScript. The browser reads it from top to bottom, running each line.

Console message

console.log("Hello from app.js!");

This line prints text to the DevTools console. It proves the file was found and executed.

A helpful analogy

An HTML page is a house. The <script> tag is the front door, and the src attribute is the address on the door telling the electrician where to find the tools. If the address is wrong, the electrician never arrives and the lights stay off.

Nesting rules

  • 1.Place the <script> tag inside the body, just before the closing </body> tag, so the page content loads first.
  • 2.Write the file path in src exactly as it is on disk, including the .js ending.
  • 3.Keep the HTML file and the JavaScript file in the same folder to start, so the path is simply the file name.

Common mistakes

Wrong file path in src

Avoid

<script src="scripts/app.js"></script> // file is in same folder

Use instead

<script src="app.js"></script>

The browser looks for the file at the path you gave. If it is not there, the console shows a 404 error and no code runs.

Forgetting the closing script tag

Avoid

<script src="app.js">

Use instead

<script src="app.js"></script>

The browser needs the closing tag to know where the script block ends. Without it the page structure breaks.

Quick tips

  • •Open DevTools and check the Console tab after loading the page to see the script's messages.
  • •If nothing appears, look for red error lines in the console; they usually name the file.
  • •Rename the script file and update src to see what a broken path looks like, then fix it.
Previous
Next