Skip to content
absolute beginner

Your First JavaScript Program

The fastest way to learn JavaScript is not to read about it. It is to write one line, run it, and watch the browser answer back. That first exchange—your…

Published 2026-05-14Updated 2026-10-047 min read
Aerial view of a sunny beach in Corpus Christi with waves and coastal buildings.
Aerial view of a sunny beach in Corpus Christi with waves and coastal buildings. Photo by Hameen Reynolds on Pexels.

The fastest way to learn JavaScript is not to read about it. It is to write one line, run it, and watch the browser answer back. That first exchange—your code in, a message out—is the whole loop that every bigger program builds on.

What You Will Build

By the end of this tutorial, you will have written and run your first JavaScript program in the browser: a page that greets you with a popup message the moment it loads.

You do not need any previous coding experience. You need a modern web browser, a plain text editor, and about ten minutes. If your tools are not set up yet, get the browser and editor ready first.

Why Start in the Browser

A six-step loop showing Write code, Save file, Open in browser, Run JavaScript, Observe output, and Change code, with arrows returning from Change code to Write code.
The browser gives you a fast learning loop: write a little code, run it, read the result, and change one thing at a time.

Your browser already contains a JavaScript engine—the software that reads and runs JavaScript code. Chrome, Firefox, Edge, and Safari all ship with one built in. That means you do not need to install a separate runner to see your first program work.

This is the fastest possible feedback loop: write a file, open it, and the browser executes your code immediately. No compiler step, no server, no waiting. You type, you run, you observe. That loop is the habit you will reuse for every program after this one.

Note: JavaScript and Java are different languages that share part of a name. Nothing you do here depends on Java, and you do not need to install it.

Knowledge check

Check your understanding

Answer this question before you continue.

Why can this tutorial run JavaScript by opening an HTML file in a modern browser?
Single Choice

Focus: Identify why a beginner can run JavaScript without installing a separate runner.

Write Your First JavaScript Program

The classic first program is often called a JavaScript hello world. It does one thing: prints a message so you can confirm your code actually runs.

Step 1: Create an HTML file

Open your text editor and create a new blank file. Save it as first-program.html. The .html extension matters—it tells your browser to treat the file as a web page.

Knowledge check

Check your understanding

Answer this question before you continue.

You saved the tutorial file as `first-program.txt`, and double-clicking it does not open it as the intended web page. What should you change first?
Debugging

Focus: Diagnose a file-naming problem that prevents the browser from treating a first program as an HTML page.

Step 2: Add the code

Type or paste this into the file:

<!DOCTYPE html>
<html>
  <head>
    <title>My First JavaScript Program</title>
  </head>
  <body>
    <script>
      alert('Hello, world!');
    </script>
  </body>
</html>

Save the file. Then find it on your computer and double-click it. Your browser opens the page and shows a popup box that reads Hello, world!

That popup is your program's output. You wrote one instruction, the browser ran it, and you saw the result. That is your first JavaScript program running.

Knowledge check

Check your understanding

Answer this question before you continue.

When the page loads with the tutorial's `alert('Hello, world!');` line inside a `<script>` element, what happens?
Output Prediction

Focus: Predict the visible result of running the tutorial's alert statement.

alert('Hello, world!');

What each line does

Most of this file is HTML—the structure of the page. Only two parts matter for your JavaScript:

  • The <script> tag tells the browser, "Everything inside here is JavaScript. Run it."
  • The line alert('Hello, world!'); tells the browser to display a popup with that message.

The alert() part is a command. The text inside the parentheses and quotes is the message it shows. Change the message, save, and refresh the page to see your edit take effect.

Common mistake: Forgetting the closing </script> tag. If your code does not run, check that both the opening and closing script tags are present and spelled exactly.

Knowledge check

Check your understanding

Answer this question before you continue.

Your JavaScript does not run because the opening `<script>` tag is present but its matching closing tag is missing. Which edit follows the article's fix?
Debugging

Focus: Fix a missing closing script tag so the browser can recognize the JavaScript block.

Run Your Code in the Browser

Opening the file by double-clicking it is the simplest way to run your program. Here is the full sequence:

  1. Save first-program.html on your computer.
  2. Double-click the file to open it in your default browser.
  3. Watch for the popup that says Hello, world!

If nothing happens, work through this checklist:

  • Did the file end in .html? Without it, the browser may not treat the file as a web page.
  • Did you copy the code exactly? A missing quote or a misspelled command stops the script.
  • Did you save after editing? An unsaved change never reaches the browser.

Tip: After you edit the file, refresh the browser tab (press F5 or Ctrl+R) rather than double-clicking the file again. Refreshing re-runs the page's JavaScript.

Understand What Your Program Did

When you opened the file, the browser loaded the HTML, reached the <script> element, and ran the JavaScript inside it as part of loading the page. That is the whole mechanism: loading the page is what triggers your code to run. The alert() command then produced visible output—the popup box.

Two small details are worth noticing now because they appear in nearly every JavaScript program you will write:

  • Statements end with a semicolon. The ; marks where one instruction stops and the next begins. JavaScript often runs without it, but adding semicolons keeps your intent clear and avoids surprises.
  • Quotes wrap text. Both single quotes ('Hello') and double quotes ("Hello") work, as long as you use the same kind on both ends of the message.

The popup is the simplest visible proof that your code runs, which is why it is a good choice for a first run. But it is not how real web pages talk to people. On a real site, the page content, buttons, and interface elements are what a user sees and interacts with.

console.log() is different again. It is a developer-facing channel—a quiet place where you, the person writing the code, can inspect values while you build. A visitor to your page will never see it. You will.

Replace the alert() line with this:

console.log('Hello, world!');

Save and refresh the page. This time no popup appears. Instead, open your browser's developer tools (press F12, or right-click the page and choose Inspect), then click the Console tab. You will see Hello, world! printed there.

The difference matters:

CommandWhere output appearsBest for
alert()A popup box that blocks the pageQuick visible proof during early learning
console.log()The developer consoleInspecting values and debugging while you build

Here is the decision rule in one line: use alert() when you want a beginner-friendly visible confirmation, and use console.log() when you want to inspect what your code is doing without interrupting the page for a user.

Common mistake: Looking for console.log() output on the page itself. It never appears there. It only shows up in the developer console.

Practice Your First Program

Now that the loop works, make it yours. Each of these drills changes one small thing and asks you to predict the result before you run it:

  1. Change the message. Edit the text inside alert() to greet yourself by name. Predict what you will see, then refresh.
  2. Add a second alert. Put another alert() line below the first. Predict the order the two popups appear, then refresh to check.
  3. Switch to the console. Replace alert() with console.log() and watch the output land in the developer console instead of a popup.
  4. Break it on purpose. Remove the closing quote from the message and refresh. Read the error the console reports. Fixing that mistake teaches you more than a perfect first run ever will.

Where to Go Next

You have written and run your first JavaScript program. The next step is to make that code do something visible on the page instead of in a popup—which means placing JavaScript inside a real web page and connecting it to buttons, forms, and page content.

For now, run the practice drills above. The goal is not to memorize syntax. It is to build the habit that carries you through everything else: write a little, run it, read the output, and change one thing at a time.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

After replacing `alert('Hello, world!');` with `console.log('Hello, world!');`, where should you look for the message?
Question 1 of 2Misconception Check

Focus: Distinguish developer-console output from output rendered on the page.

console.log('Hello, world!');
After editing and saving `first-program.html`, what should you do to run the updated JavaScript in the already open browser tab?
Question 2 of 2Single Choice

Focus: Apply the tutorial's workflow for running a saved change to a browser-based JavaScript program.

References

  1. A first splash into JavaScript - Learn web development | MDNdeveloper.mozilla.org
  2. Hello, World! Your First JavaScript Programs — SitePointwww.sitepoint.com
  3. Write your first JavaScript code | Opensource.comopensource.com
Practical resource

Want a more structured JavaScript path?

Use the JavaScript for AI Applications Starter Pack to turn individual tutorials into a focused path from language fundamentals to interactive AI applications.

View the bundle
Coming soon

JavaScript for AI Applications Starter Pack

Build the JavaScript foundation behind modern interactive applications. The JavaScript for AI Applications Starter Pack takes you from core language concepts through functions, browser events, forms, DOM updates, application state, debugging, and practical projects—so you can learn how interfaces take input, work with structured data, respond to users, and turn results into experiences people can actually use.

$9
PDF BundleJavaScriptWeb DevelopmentAI Applications
  • 253-page illustrated PDF
  • 12 guided JavaScript chapters
  • Visual concept diagrams
  • Self-assessment quizzes
  • Bonus deep-dive sections
  • DOM, events, forms & application state
  • Browser projects, debugging & practical workflows

Coming soon

Keep learning

Related tutorials

Continue with nearby JavaScript topics and beginner-friendly explanations.