Francis Landford.
← JavaScript for Beginners: Make Your Web Pages Interactive
code

What JavaScript Is and How to Run It

JavaScript is the programming language of the web. Every browser can run it, so you do not need to install anything.

Where JavaScript runs

Browsers run JavaScript on the visitor's device. It can react to clicks, check forms, change the page and talk to servers.

Method 1: the browser console

Open any page, press F12, and choose the Console tab. Type this and press Enter:

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

console.log() prints a message. It is the programmer's best friend for checking what code is doing.

Method 2: inside an HTML file

Create index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>My JavaScript Page</title>
  </head>
  <body>
    <h1>JavaScript test</h1>

    <script>
      console.log("The page has loaded.");
      alert("Welcome!");
    </script>
  </body>
</html>

Open it in a browser. You will see an alert box, and the message appears in the console (F12).

Method 3: a separate file

Real projects keep code in its own file. Create app.js and link it at the bottom of the body:

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

Putting the script after your content means the page elements already exist when the code runs.

Comments

Notes for humans that the computer ignores:

// This is a single-line comment
/* This is a
   multi-line comment */

Reading error messages

When something breaks, the console shows a red message with a file name and line number. Read it. It usually tells you exactly what is wrong.

Your task

Make an index.html and an app.js file. In app.js write console.log("My name is ..."); with your own name, open the page, and check the console.