Your First JavaScript Program

Updated · 7 min read
easyES2026javascriptscript-tagdeferoutputbasics

A browser runs JavaScript when it finds a <script> element in an HTML page. The script can be written inside the page (inline) or kept in its own .js file (external). Where you put the tag and which attributes you give it decide when the code runs, and that decides whether your code can find the elements it wants to change. This lesson builds the first Fee Tracker page and shows the four ways a program can show output.

In One Minute

  • Put JavaScript in a page with <script>: inline code between the tags, or an external file with src="app.js".
  • Use defer (or type="module") on external scripts in the <head>: the script downloads early and runs after the page's HTML is ready.
  • Show output with console.log (for developers), element.textContent (for readers), and occasionally alert.
  • document.write is the older way: don't use it in new code.

The Script Element

Inline Scripts

Code between <script> and </script> runs when the browser reaches it while reading the page from top to bottom.

inline.html
<h1>Fee Tracker</h1>
<p id="message"></p>
<script>
  document.querySelector("#message").textContent = "Welcome, Navika!";
</script>

The script comes after the <p>, so when it runs, the paragraph already exists and the page shows "Welcome, Navika!".

External Scripts

Real projects keep JavaScript in separate files. The page loads it with src:

index.html
<script src="app.js" defer></script>
BenefitWhy it matters
SeparationHTML describes content; JavaScript lives where your editor and tools expect it
CachingThe browser downloads app.js once and reuses it on every page that uses it
ReuseMany pages can share one script

When Scripts Run: Defer, Async and Modules

By default, when the browser meets <script src="...">, it stops building the page, downloads the file, runs it, and only then continues. A script in the <head> therefore runs before the <body> exists, and document.querySelector finds nothing.

Two attributes and one script type change that:

Script tagDownloadsRunsOrder between scripts
<script src>Blocks the pageImmediatelyIn page order
<script src defer>In parallel with the pageAfter the HTML is fully parsedIn page order
<script src async>In parallel with the pageAs soon as it arrivesNot guaranteed
<script type="module" src>In parallel with the pageAfter the HTML is parsed (deferred by default)In page order

For the code you write, the rule is short: put scripts in the <head> with defer, or use type="module". Use async only for independent scripts such as analytics, which don't touch your page or other scripts.

Four Ways to Show Output

MethodWho sees itUse it for
console.log(value)Developers, in the consoleChecking values while you build and debug
element.textContent = textReaders, on the pageReal output in a web page
alert(text)Readers, in a popup that blocks the pageRarely; quick tests only
document.write(text)Readers, but it can erase the pageOlder way: don't use it

Printing to the Console

console.log prints any number of values. It is the tool you will use most while learning.

output.js▶ Try it
const student = "Srihitha Anamaneni";
const grade = 10;
const fee = 150;
 
console.log("Student:", student);
console.log("Grade", grade, "fee", fee);
console.log(["Navika", "Advitha"]);
Output
Student: Srihitha Anamaneni
Grade 10 fee 150
['Navika', 'Advitha']

Explanation: each call prints one line. Values separated by commas are printed with spaces between them. An array prints with its items in square brackets, and strings inside it are shown in quotes so you can see where each one starts and ends.

console has more methods: console.error and console.warn print messages styled as errors and warnings in the browser, and console.table prints an array of objects as a table. The debugging lesson covers them.

Writing Text to the Page

To show output to the reader, find an element and set its textContent. This is how real pages show results:

greeting.html
<p id="greeting"></p>
<script>
  const name = "Advitha";
  document.querySelector("#greeting").textContent = `Hello, ${name}!`;
</script>

The page shows Hello, Advitha!. textContent always treats what you give it as plain text, which makes it safe even when the text came from a user. Its cousin innerHTML turns text into HTML, which is dangerous with user input; the lesson on changing the DOM explains why.

Alerts and the Older Ways

alert("Saved") shows a popup and freezes the page until the reader clicks OK. It is fine for a two-second test, but real pages use a message on the page or a <dialog> instead.

document.write writes HTML into the page while it loads. Called after the page has loaded (for example in a click handler), it wipes out the whole page. You will still see it in old tutorials; don't copy it.

In Practice

Here is the first version of the Fee Tracker: a page with a heading, a summary line, and a script that fills it in. This is also the checkpoint for this chapter.

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Fee Tracker</title>
    <script src="app.js" defer></script>
  </head>
  <body>
    <h1>Fee Tracker</h1>
    <p id="summary">Loading...</p>
  </body>
</html>
app.js
const reader = "Navika";
const studentsTracked = 3;
 
console.log("Fee Tracker started for", reader);
document.querySelector("#summary").textContent =
  `Welcome, ${reader}. You are tracking ${studentsTracked} students.`;

Because the script has defer, it runs after the <p id="summary"> exists, even though the tag is in the <head>. The console shows Fee Tracker started for Navika, and the page shows "Welcome, Navika. You are tracking 3 students."

Common Mistakes

Script in the head without defer. The script runs before the body exists, and the line document.querySelector("#summary").textContent = ... fails with TypeError: Cannot set properties of null (setting 'textContent'). Add defer.

Wrong path in src. If the file isn't found, nothing runs and there is no error in your code; look in the console for a red "404" or "Failed to load resource" message. Check the file name and folder.

Using async for scripts that depend on each other. async scripts run in whatever order they arrive, so a script that uses another script's functions can fail at random. Use defer.

Expecting console.log to show anything on the page. It only writes to the console. Use textContent to show output to readers.

Practice

Challenge: Write the JavaScript part of a page that has <p id="fee"></p>. Store a student name and a fee, print a line to the console, and build the text the page would show. Below, console.log stands in for the page so you can run it here.

Show solution
fee-line.js▶ Try it
const student = "Advitha Anamaneni";
const fee = 95;
const line = `${student} owes $${fee}.`;
 
console.log("Building the fee line");
console.log(line); // on the page: document.querySelector("#fee").textContent = line;
Output
Building the fee line
Advitha Anamaneni owes $95.

Quick check: Your first program

3 questions
1Where should an external script that changes the page go?
2Which script runs as soon as it downloads, in no guaranteed order?
3Which way of showing output is meant for readers of the page?

Key Takeaways

You want toWrite
Load a script after the HTML is ready<script src="app.js" defer></script>
Load a module<script type="module" src="app.js"></script>
Print for developersconsole.log("value", x)
Show text on the pageelement.textContent = "text"

Was this tutorial helpful?

Lesson 4 of 139