Printing

Welcome to the JavaScript course! JavaScript is the programming language of the web: when a page reacts to your clicks, shows a menu or checks what you typed into a form, it's usually JavaScript at work. It runs in every web browser, and it's one of the most widely used programming languages in the world.

A program is a list of commands that the computer carries out one after another, from top to bottom. Your first command will show something on the screen. A program often calculates things that nobody can see, so printing the results is how you find out what it did and whether it works.

JavaScript prints to the console: a panel that shows what a program prints, and also its errors. Here, it's the black panel on the right (on a phone, under Output). To print something, write console.log() and put what you want to print inside the parentheses:

console.log(4);
console.log("Hello, world!");

This prints:

4
Hello, world!

Each console.log() prints on a new line, and the program runs them in the order in which they are written.

Notice two things:

  • Text goes in quotes, numbers don't. The quotes show JavaScript where the text starts and ends, so that it doesn't try to read the words as commands. You can use double quotes "Hello" or single quotes 'Hello'; in this course, we use double quotes. Numbers are written without quotes: 4.
  • A statement ends with a semicolon ;, as a sentence ends with a period. A statement is one command for the computer, such as one console.log(). JavaScript can often tell where a statement ends even without a semicolon, but not always, so we always write it.

Write console.log exactly like this: all in lowercase, with a dot between console and log. JavaScript treats uppercase and lowercase letters as different, so Console.log wouldn't work.

Instructions

In the editor on the right, print your age with console.log(). It's a number, so write it without quotes. Then click Run below the editor.

On the next line, print your name. It's text, so put it in quotes.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/15

Next