Show Output With console.log() and Comments

In Write and Run Your Own Script, you created scratch/hello.js, ran it with node, changed its message, saved, and ran it again. That one line did its job, but you copied it without knowing what each part means. Now it’s time to take that line apart so you can write lines like it yourself.

By the end of this lesson, you’ll be able to write a short script that prints text and calculations, shows several values on one line, and uses comments both to explain a line and to switch it off. You’ll work in your own hello.js the whole time, and you’ll run it after every saved change, so the terminal tells you right away whether each change did what you expected.

Take a console.log() Line Apart

Open scratch/hello.js in VS Code. It holds the one line from last time, with whatever message you gave it. Here it says this:

Language: JavaScript Filename: scratch/hello.js
console.log("Tonight we're watching Spirited Away!");

Run it from the terminal in your js-course folder, just like before:

Language: Shell
$ node scratch/hello.js
Tonight we're watching Spirited Away!

Here’s what each part of that line does:

  • console.log() is JavaScript’s way of saying “show this in the terminal.” The console part is a built-in console tool for messages, and .log asks it to write one line.
  • The parentheses hold what you want to show. Whatever sits between ( and ) is what ends up in the terminal.
  • The quotes mark the text, as they did in the REPL. They aren’t part of the message, which is why the output shows Tonight we're watching Spirited Away! without them.
  • The semicolon (;) at the end closes the instruction, a bit like a period closes a sentence.

One complete instruction like this is called a statement. JavaScript would often run a statement without its semicolon, but ending each one with ; is the common practice, and it’s the one you’ll follow in this course.

Why does a script need console.log() at all? In the REPL, Node echoed the answer to every line you typed. A script doesn’t do that. It shows only what you ask it to show with console.log(). You’ll look at that difference more closely in the next lesson.

You can try console.log() in the REPL too, and you’ll spot something extra. Start node on its own and type this line:

Language: JavaScript
> console.log("Hello!")
Hello!
undefined

The first line, Hello!, is what console.log() printed. The second line, undefined, is the REPL’s echo, like the 5 it echoed for 2 + 3. Here it means “no value,” because console.log() shows something but doesn’t produce an answer. It’s not an error, and it won’t appear when the same line runs in a script. Leave the REPL with .exit before you go on.

Show Text or Show a Calculation

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.