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:
| |
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:
| |
Running Locally
If you have Node.js installed (v18+):
| |
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:
| |
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:
| |
Why JavaScript Stands Out
Compare JavaScript’s Hello World to other languages:
| Language | Lines | Requirements |
|---|---|---|
| JavaScript | 1 | None |
| Python | 1 | None |
| Java | 5 | Class, main method |
| C | 4 | #include, main function |
| Rust | 4 | fn 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
- JavaScript is interpreted - No compilation step; code runs directly
- Dynamic typing - Variables don’t need type declarations
- Runs everywhere - Browsers, servers (Node.js), mobile (React Native), desktop (Electron)
- 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:
| |
The Node.js REPL
You can also use Node.js interactively:
| |
Then type:
| |
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.