Beginner

Hello World in JavaScript

Your first JavaScript program - the classic Hello World example with Docker setup using Node.js

Every programming journey starts with Hello World. JavaScript makes this remarkably simple - just one line using the console.log() function.

The Code

Create a file named hello.js:

1
console.log("Hello, World!");

That’s it! One line, no boilerplate, no imports required.

Understanding the Code

  • console.log() - A built-in function that outputs text to the console
  • "Hello, World!" - A string literal enclosed in double quotes
  • No semicolon required - JavaScript has automatic semicolon insertion (ASI), though many developers add them explicitly
  • No main function - JavaScript executes code from top to bottom

Running with Docker

The easiest way to run JavaScript without installing Node.js locally:

1
2
3
4
5
# Pull the official Node.js Alpine image
docker pull node:22-alpine

# Run the program
docker run --rm -v $(pwd):/app -w /app node:22-alpine node hello.js

Running Locally

If you have Node.js installed (v18+):

1
2
# Run directly with Node.js
node hello.js

Expected Output

Hello, World!

Running in the Browser

JavaScript is unique - it can run directly in your web browser. Open your browser’s Developer Tools (F12 or Cmd+Option+I on Mac) and type in the Console:

1
console.log("Hello, World!");

You’ll see the output immediately. This is the JavaScript REPL (Read-Eval-Print Loop).

Alternative Ways to Print

JavaScript offers several ways to output text:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Standard console output
console.log("Hello, World!");

// With formatting (in browsers)
console.info("Information message");
console.warn("Warning message");
console.error("Error message");

// In a browser, you can also use:
// alert("Hello, World!");  // Shows a popup dialog
// document.write("Hello, World!");  // Writes to the page

Why JavaScript Stands Out

Compare JavaScript’s Hello World to other languages:

LanguageLinesRequirements
JavaScript1None
Python1None
Java5Class, main method
C4#include, main function
Rust4fn main, println! macro

JavaScript and Python tie for simplicity, but JavaScript has the unique advantage of running in every web browser without any installation.

Key Concepts

  1. JavaScript is interpreted - No compilation step; code runs directly
  2. Dynamic typing - Variables don’t need type declarations
  3. Runs everywhere - Browsers, servers (Node.js), mobile (React Native), desktop (Electron)
  4. Event-driven - JavaScript excels at handling user interactions and async operations

Modern JavaScript Features

Even in a simple Hello World, you could use modern syntax:

1
2
3
4
5
6
7
// Using template literals (ES6+)
const greeting = "World";
console.log(`Hello, ${greeting}!`);

// Using arrow functions
const sayHello = () => console.log("Hello, World!");
sayHello();

The Node.js REPL

You can also use Node.js interactively:

1
docker run --rm -it node:22-alpine node

Then type:

1
2
3
4
> console.log("Hello, World!")
Hello, World!
undefined
> .exit

The undefined you see is the return value of console.log() - it prints the message but returns nothing.

Next Steps

Continue to Variables and Data Types to learn about storing and manipulating data in JavaScript.

Running Today

All examples can be run using Docker:

docker pull node:22-alpine
Last updated: