Your First JavaScript Program

JavaScript ES2024 Β· βœ“ verified by execution on 2026-08-04

A program is a list of instructions that a computer follows one line at a time, top to bottom. You are about to write your first one β€” and run it right here in the page.

Your first line of JavaScript

The instruction console.log() outputs a message to the console. Whatever you put between the quotation marks appears on screen. Press Run β€” then change the message and run it again.

javascript
console.log("Hello, World!");
Output
Hello, World!

That’s a complete JavaScript program. One line is all it takes.

How JavaScript reads your line

Three parts matter:

console.log("Hello, World!"); The instruction String literal (text in quotes)
Anatomy of a console.log statement
  1. console.log β€” the name of a built-in instruction.
  2. The parentheses () β€” they hand your text to console.log.
  3. "Hello, World!" β€” text wrapped in quotation marks. Text in quotes is called a string literal: JavaScript displays it exactly as written and never tries to interpret it.

You can hand console.log several strings at once, separated by commas β€” it joins them with spaces:

javascript
console.log("JavaScript", "is", "fun");
Output
JavaScript is fun

Single quotes work exactly like double quotes β€” pick one style and stay consistent:

javascript
console.log('Single quotes work too');
Output
Single quotes work too

Common Misconceptions

A very common mistake when learning JavaScript is forgetting the quotation marks around your text. When you forget the quotes, JavaScript thinks you are referring to a variable, not a literal string of text. If no variable by that name exists, your code will crash.

Another common point of confusion is how console.log handles multiple pieces of text. When you pass multiple strings separated by commas, JavaScript joins them together.

Try to predict the outcome below. Does JavaScript add anything between the words?

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

console.log("Hello", "World");
Output
Hello World

Your first errors (everyone gets them)

Errors are not failure β€” they are JavaScript telling you exactly what to fix. Learn to read them from day one and you will debug faster than most learners. Two you will meet this week:

1. SyntaxError β€” JavaScript can’t understand the line. Here the closing parenthesis is missing, so JavaScript stops before running anything:

javascript
console.log("Hello, World!";
This example raises an error (on purpose)
SyntaxError

The message usually tells you what went wrong. Fix the typo, run again.

2. ReferenceError β€” JavaScript doesn’t know that name. As you saw earlier, if you capitalize console by accident, JavaScript thinks you are asking for a variable named Console that you never created:

javascript
Console.log("Hello");
This example raises an error (on purpose)
ReferenceError

JavaScript is case-sensitive, so Console and console are entirely different things.

Edge cases worth knowing

Check yourself

What does the console.log() method do?

Reveal answer

Writes text to the developer console (the screen) β€” In JavaScript, console.log() outputs a message to the web console. It has nothing to do with paper printers.

What happens if you run console.log(Hello) β€” without quotation marks?

Reveal answer

JavaScript raises a ReferenceError, because it looks for a variable named Hello β€” Without quotes, JavaScript treats Hello as a variable name. No such variable exists yet, so you get a ReferenceError.

Which line makes JavaScript display exactly 2 + 2 (and not 4)?

Reveal answer

console.log("2 + 2"); β€” Quotation marks make something a string β€” literal text. console.log("2 + 2") displays the text as-is; without quotes, JavaScript would calculate the math first.

Is Console.log("hi") the same as console.log("hi")?

Reveal answer

No β€” JavaScript is case-sensitive, so Console raises a ReferenceError β€” JavaScript names are case-sensitive: console exists, Console does not. Capitalizing it raises a ReferenceError.

Challenges

Challenge 1 +10 XP

Make JavaScript display exactly this sentence: I am learning JavaScript

javascript
  • Test 1 β€” expects "I am learning JavaScript"
Need a hint? (βˆ’25% XP)

Put the whole sentence inside quotation marks, inside console.log().

Show solution (0 XP)
console.log("I am learning JavaScript");

Challenge 2 +15 XP

Print a three-line introduction. Line 1: Hello! β€” Line 2: My name is JS. β€” Line 3: Nice to meet you.

javascript
  • Test 1 β€” expects "Hello!\nMy name is JS.\nNice to meet you."
Need a hint? (βˆ’25% XP)

Each console.log() starts a new line automatically.

Show solution (0 XP)
console.log("Hello!");
console.log("My name is JS.");
console.log("Nice to meet you.");