Run your first JavaScript

ECMAScript 2026

JavaScript is a programming language. It gives the browser instructions it can run, such as calculating a price or changing a message after a button click. Start by making the browser write one message.

On this page

Use the browser Console

Developer Tools lets you inspect a web page. The Console tab lets you run JavaScript and see messages from the page. console.log() is an instruction for printing a message there. The quotes mark the text you want to print.

How to try it Open a page in your browser. Right-click the page and choose Inspect, find the Console tab, type this line, and press Enter.

console.log("Your order is ready.");

What to expect The Console shows “Your order is ready.”. It may also show undefined as the result of the instruction itself.

Place JavaScript in a <script> element

The browser executes code inside a script element when it reaches that element. This example places the script near the end of body. The paragraph belongs to the visible page; the log goes to the Console.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Place JavaScript in a &lt;script&gt; element</title>
</head>
<body>
<p>Product catalog</p>
  <script>
    console.log("The catalog page loaded.");
  </script>
</body>
</html>

What to expect “Product catalog” appears on the page. Open Console and reload to see “The catalog page loaded.”.

Link a separate JavaScript file

A .js file can contain the JavaScript for a page. src on a script element gives the browser the file path. With a classic external script in head, defer tells the browser to wait until it has finished reading the HTML before running it.

How to try it Save this line in app.js beside your HTML file. Remove the old inline <script> block and add <script defer src="app.js"></script> inside <head>. Save both files and reload.

console.log("app.js is linked.");

What to expect After reloading, the Console logs “app.js is linked.”.

If the result differs Check the filename and the browser Console for a load error if the new message is missing.

Official references