JavaScript / Getting started

Printing to the console

Write your first program that prints text, see what an error looks like, and leave notes with comments.

A program is a list of instructions the computer follows from top to bottom. We write it in the editor, and the result appears in the console.

To print text, write console.log followed by the text in quotes inside brackets. Change the text, then press Run:

JavaScript
console.log("Hello");
console.log("Good morning");
Console
Hello
Good morning
Each line ends with a semicolon ;. Text must be inside quotes, otherwise you get an error:
JavaScript
console.log(Good luck);
Console
SyntaxError: missing ) after argument list

Comments

A line that starts with // is a comment: a note for you that the computer ignores. It is also handy for switching a line off without deleting it.

JavaScript
// Shopping list
console.log("Eggs");
// console.log("Apple");
console.log("Milk");
Console
Eggs
Milk

Practice

1Write the programCurriculum practice

Print Good night in the console.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Edit the programCurriculum practice

This program shows an error. Fix it so it prints Good luck.

HintText needs quotes around it.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
console.log(Good luck);
Console
Press Run to see the result.
3Edit the programCurriculum practice

Turn the line that prints Fish into a comment so it is not printed.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
console.log("Lion");
console.log("Fish");
console.log("Elephant");
Console
Press Run to see the result.
4Write the programExtra practice

Print your name on the first line and your city on the second. (The solution uses Mona and Cairo.)

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
5Write the programExtra practice

Print this shape on three lines:
*
**
***

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • console.log("text"); prints text in the console.
  • Text goes inside quotes, and each line ends with ;.
  • // starts a comment the computer ignores.