LearnFrenzy
Sign in Take a free test

Getting Started · Lesson 2 of 40

How to Run JavaScript: Your First Program

4 min read Updated Nov 7, 2026 Saurabh Samir

You don't need to install anything to start with JavaScript – every browser can run it. For this course you'll use three places: the browser console for quick experiments, an HTML page for web code, and Node.js for running files in the terminal. This lesson sets all three up.

In this lesson

  • The browser developer console
  • Adding JavaScript to an HTML page
  • Installing Node.js and running a file
  • VS Code, statements, comments and errors

1. The browser console

Open any page, press F12 (or Ctrl+Shift+J in Chrome, ⌘+⌥+J on a Mac) and choose the Console tab. Type and press Enter:

> 2 + 3
5
> "Learn".toUpperCase()
'LEARN'
> new Date().getFullYear()
2026

The console is perfect for trying one line. Every lesson example in this course also has a ▶ Run button that runs it right on the page.

2. JavaScript in an HTML page

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My first script</title>
  <script src="app.js" defer></script>
</head>
<body>
  <h1 id="title">Hello</h1>
</body>
</html>
// app.js
document.getElementById("title").textContent = "Hello from JavaScript!";
console.log("Page updated");
  • Keep JavaScript in its own .js file and load it with <script src>.
  • defer runs the script after the HTML has been parsed, so elements like #title already exist. Use type="module" instead to load it as an ES module (also deferred).

3. Node.js: JavaScript in the terminal

Install the LTS version from nodejs.org, then check it:

node --version
npm --version

Create hello.mjs and run it:

const learner = "Rohan";
const languages = ["JavaScript", "SQL", "Git"];

console.log(`Hi ${learner}! This week: ${languages.join(", ")}`);
console.log("Languages:", languages.length);
node hello.mjs

Output

Hi Rohan! This week: JavaScript, SQL, Git
Languages: 3

About this course's examples

Examples run as ES modules (the .mjs extension, or type="module" in a browser). That is how modern JavaScript is written: it enables import/export and turns on strict mode, which reports silent mistakes as errors. Outputs are shown the way Node prints them – browsers format arrays and objects slightly differently, but the values are the same.

VS Code

Install Visual Studio Code, open your project folder (File → Open Folder) and run files from its built-in terminal (Ctrl+`). Useful extensions: ESLint (catches mistakes) and Prettier (formats code). The Live Server extension reloads your HTML page automatically when you save.

Statements and comments

// A single-line comment
/* A multi-line
   comment */
let total = 40 + 2;          // a statement ends with a semicolon
console.log("Total:", total);
console.log("Total: " + total);   // + joins strings
console.log(`Total: ${total}`);   // template literal – the modern way

Output

Total: 42
Total: 42
Total: 42

Semicolons are technically optional thanks to "automatic semicolon insertion", but it has edge cases – most teams either always write them or use a formatter that adds them. This course writes them.

Reading errors

When something goes wrong, JavaScript throws an error with a type, a message and a stack trace showing the file and line:

const user = { name: "Asha" };
console.log(user.name.toUpperCase());
console.log(user.address.city);

Output

ASHA
TypeError: Cannot read properties of undefined (reading 'city')
ErrorTypical cause
ReferenceError: x is not definedmisspelt or undeclared variable
TypeError: … is not a functioncalling something that isn't a function
TypeError: Cannot read properties of undefinedusing a property of a missing value
SyntaxError: Unexpected tokenmissing bracket, quote or comma

Practice

Try it yourself

  1. Print your name, city and favourite language on one line using a template literal.

    Show solution
    const name = "Meera", city = "Kochi", lang = "JavaScript";
    console.log(`${name} from ${city} loves ${lang}`);

    Output

    Meera from Kochi loves JavaScript

Quick check

What does the defer attribute do?

It downloads the script in parallel and runs it after the HTML is parsed, so page elements exist.

Which command runs app.mjs in the terminal?

node app.mjs

What does "Cannot read properties of undefined" usually mean?

You accessed a property of a value that is undefined – often a missing object or a typo.

Summary

  • Use the browser console for quick tests, a script tag (with defer) for pages, and Node.js for files.
  • This course runs examples as ES modules (strict mode); use template literals for output.
  • Read errors by type and message; the stack trace points to the line.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Popular