JavaScript - 1

JavaScript - 1: Your First JavaScript

JavaScript lets you add instructions and behavior to a webpage. In this first lesson, you will use console.log() to make JavaScript print a message.

The console is commonly used by developers to display information while writing and testing code. In this course, your console.log() messages will appear directly in the Live Output panel.

Your challenge: Print "Hello, JavaScript!", then experiment by printing a few messages of your own.

✦ JavaScript Editor
▶ Live Output

How does console.log() work?

A JavaScript instruction such as: console.log("Hello, JavaScript!"); tells JavaScript to send a value to the console.

In this lesson, the editor captures those console messages and displays them in the Live Output panel so you can immediately see what your code produced.

What are the quotation marks for?

The words "Hello, JavaScript!" are text. In JavaScript, a piece of text like this is called a string.

Quotation marks tell JavaScript where the string begins and ends.

For example: console.log("Mission started!"); prints the text: Mission started!

Try more than one message:

JavaScript runs these instructions from top to bottom.

Try: console.log("First message"); followed by: console.log("Second message");

Both messages should appear in the output in the same order.

What does the semicolon do?

The semicolon at the end of: console.log("Hello!"); marks the end of the statement.

JavaScript can sometimes work without semicolons, but using them consistently is a clear habit for beginners.

Experiment: Change the message without changing the rest of the instruction.

For example: console.log("A tiny robot says hello.");

Then try printing three different messages on three separate lines of code.

Try printing a number:

JavaScript can log more than text. Try: console.log(42);

Notice that the number does not need quotation marks. You will learn more about the difference between text and numbers in upcoming lessons.

Make a mistake on purpose:

Remove one quotation mark from your message and look at the Live Output. JavaScript should report an error instead of running the broken instruction.

Then restore the quotation mark and watch the output work again. Learning to recognize and fix errors is an important part of programming.

Good JavaScript habit: Make one small change at a time and watch what happens. Small experiments make it easier to understand which part of the code caused the result.

👀 Show Solution
console.log("Hello, JavaScript!");

console.log("My first JavaScript program is running!");

The first statement prints the required message. The second demonstrates that you can call console.log() again with a different string.

JavaScript runs the first statement and then continues to the next one, so both messages appear in the output.