Lesson 1 of 34 · Fundamentals
Hello, World! Your First JavaScript Program
Every programmer's journey starts with the same two words: Hello, World! In this lesson you'll write your first JavaScript program and make the computer talk back to you.
JavaScript is the language of the web. It runs in every browser, on servers with Node.js and inside countless apps. The code you write in this course runs right here in your browser, using the same engine that powers real websites.
Printing with console.log
The quickest way to see what your program is doing is to print something. In JavaScript you do that with console.log:
console.log("Hello, World!");
When you run this, the console shows:
Hello, World!
console.log(...) is a function call. The value between the parentheses is what gets printed. You'll use console.log constantly, not just for output but to peek inside your programs while debugging.
Strings: text in quotes
Text in JavaScript is called a string. A string is wrapped in quotes, and you can choose between three kinds:
console.log("double quotes");
console.log('single quotes');
console.log(`backticks`);
All three print plain text. The opening and closing quote must match. If your text contains an apostrophe, wrap it in double quotes so the apostrophe doesn't end the string early:
console.log("It's dangerous to go alone!");
Programs run top to bottom
A JavaScript program is a list of statements that run one after another, from top to bottom. Each console.log prints on its own line:
console.log("First");
console.log("Second");
console.log("Third");
The semicolon ; marks the end of a statement. JavaScript can often insert semicolons for you, but writing them yourself is a good habit that avoids surprises.
Numbers and math
Not everything has to be a string. Numbers are written without quotes, and JavaScript calculates expressions before printing them:
console.log(2 + 3); // prints 5
console.log("2 + 3"); // prints 2 + 3 (it's just text)
Comments
Anything after // on a line is a comment. JavaScript ignores comments; they're notes for humans. Use /* ... */ for comments that span several lines:
// This line is ignored
console.log("This runs"); // so is this note
/*
A longer explanation
can go here.
*/
Common mistakes
- Forgetting the quotes.
console.log(Hello)makes JavaScript look for something namedHelloand fails withReferenceError: Hello is not defined. - Mismatched quotes.
"Hello'is not a complete string. Open and close with the same kind of quote. - Wrong capitalisation. JavaScript is case-sensitive:
Console.logorconsole.Logwill not work.
Assignment
Write a program that prints exactly these three lines:
Hello, World!
I'm learning JavaScript.
42
- Use one
console.logcall per line. - The second line contains an apostrophe, so pick your quotes carefully.
- Print the third line by letting JavaScript calculate
40 + 2. Don't put it in quotes.
Hello World in other languages
- Python Hello World: Your First Program · Python
- Go Hello World: Your First Go Program · Go
- Java Hello World: Your First Program · Java
- C# Hello World: Your First Program · C#
Need a quick reminder? The JavaScript cheat sheet has every topic of Learn JavaScript on one page.
Lesson last updated on .