Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Which Lines Print?

In this video, you predicted which lines of a short movie-night script show something in the terminal, then watched Node run the file line by line.

Key Points

  • In Node’s REPL, every line you type gets an answer back. Type 2 * 3, and the REPL echoes 6. That echo is the REPL helping you try ideas one at a time.
  • When you run a script with node, Node starts at the top of the file and goes through it once, from top to bottom. Then the script stops.
  • A script never echoes a line’s value. A line like 2 * 3; still gets worked out, but nothing shows up. In this script, only the console.log() lines put text in the terminal.
  • Comments don’t print. Node skips a line that starts with //, including a console.log() line that a comment has switched off.
  • Text in quotes prints without its quotes. When one console.log() gets two values, Node prints them on one line with a space between.

The Script

Here’s the script from the video, saved as scratch/movie.js:

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

00:00 By now, you’ve run JavaScript in two places. In Node’s REPL, you type one line, press Enter, and an answer comes right back. In a script, you save your lines in a file and run the whole file with the node command.

00:13 Those two feel alike, but they don’t behave alike. In this video, you’ll find out which lines of a script actually show up in the terminal, and you’ll get to guess first.

00:25 Start in the REPL. Type 2 * 3, and press Enter. The REPL works it out and echoes 6.

00:34 That echo is the REPL being helpful. It shows you the value of every line you type, so you can try ideas one at a time. A script doesn’t do that, and the rest of this video shows what it does instead.

00:47 Here’s a small script for your movie night, saved as movie.js in your scratch folder. It has five lines. The first line is a comment about the evening.

00:58 The second logs when the movie starts. The third is the same 2 * 3 you typed in the REPL, for two bags of popcorn at three each. The fourth logs a label and that same calculation.

01:12 The last line would remind you to bring a blanket, but a comment switches it off. Your turn: pause the video and predict which lines of this script show something in the terminal when you run it.

01:29 Only lines two and four print.

01:33 Here’s how Node gets there. You run node with the file’s path, and Node starts at the top of the file. Line one is a comment, so Node skips it.

01:44 Line two logs its text, and Movie night starts at 8 appears in the terminal, without its quotes.

01:52 Line three works out 2 * 3. The answer is 6, but nothing shows up. A script never echoes a line’s value the way the REPL does. Line four logs two values, the label and the calculation.

02:07 Node prints them on one line with a space between, so you see Popcorn costs 6.

02:13 Line five is switched off, so Node skips it too. Then the file ends, and the script stops. One run goes through the file once, from top to bottom.

02:25 So, a script runs from top to bottom, once, and it never echoes a line’s value. In this script, only the console.log() lines put text in the terminal.

02:35 The REPL is different, because it echoes every line to help you try things out. Next up is an exercise, Print a Plant-Watering Reminder. You’ll write a few lines like these right in your browser, run them, and get automated feedback on what they print.

02:51 Run your code early and often, and read any message you get for clues.

Become a Member to join the conversation.