Loading video player…

Trying JavaScript in the REPL

Trying JavaScript in the REPL

In this lesson, you opened Node’s REPL, tried a line of the setup check in it, and left it again.

  • Run node with no arguments to open the REPL, the way python opens Python’s. Its prompt is > instead of >>>.
  • Type a line, and the REPL echoes its value, as Python’s does. Strings come back in single quotes.
  • The REPL echoes the value of every line. A console.log() line shows its output, then undefined, because console.log() returns undefined, JavaScript’s word for no value. Python’s print() returns None too, but Python’s REPL doesn’t echo None. A script never echoes, so the undefined never reaches the report.
  • The up arrow brings back your last line. The REPL’s own commands start with a dot: .help lists them, and .exit leaves. Ctrl+D leaves too.
  • node -p "expression" runs one expression from the shell and prints its value, where python -c prints only what you print().

Here’s the session from the video, after the banner:

Language: Node.js
> "request VPN access"
'request VPN access'
> console.log("1. request VPN access")
1. request VPN access
undefined

Watch out for Python’s exit(): Node’s REPL doesn’t have it, so you stay at the prompt. Type .exit or press Ctrl+D instead. For a single value, skip the REPL and use node -p:

Language: Shell
$ node -p "process.platform"
linux

This run reports linux on Linux. The value is your operating system, the one the setup check reports: darwin on macOS and win32 on Windows.

Next up: From print() to console.log(), a video where you put the setup check’s lines in a file, check-setup.js, and run it with node.

00:00 In this lesson, you’ll open Node’s REPL, try a line of the setup check in it, and leave it again. Use Node’s REPL as you use Python’s. Run node with no arguments.

00:14 It greets you with its version and a hint, then waits at its own prompt: one angle bracket instead of three. Type the same step. Node’s REPL echoes it back, in single quotes, the way Python’s does.

00:29 Trying a line works the same in both.

00:34 Now print the step the way the report will, numbered. In Python, print shows the line, and nothing else. JavaScript prints a line with console.log, which the next lesson covers.

00:49 Type the same step with it.

00:53 Two lines: the step, and then undefined.

01:00 The two lines come from two places. The first is what console.log printed: that’s output. The second is the REPL’s echo. The REPL shows the value of every line you type, and console.log returns undefined, JavaScript’s word for no value.

01:20 Python’s print returns None in the same way. The difference is the REPL: Python’s stays quiet about None, and Node’s shows undefined.

01:33 In a script, nothing is echoed, so the report gets the step and nothing else. As in Python, up recalls a line and Enter runs it. The REPL also has its own commands, and they all start with a dot.

01:49 Type .help, and it lists them.

01:54 To leave, type .exit, or press Ctrl+D. Watch out for Python’s exit(). Node’s REPL doesn’t have it, and you’d stay at the prompt. Unlike Python’s -c, Node’s -p runs one expression and prints its value for you, like a one-line REPL.

02:17 Here, it asks Node’s process object for the operating system, the value the setup check reports. This run prints linux on Linux. On macOS, it prints darwin, and on Windows, win32.

02:34 So here’s the rule. Try lines in Node’s REPL as you would in Python’s, and read the undefined after console.log as the echo, not output. Next up is From print() to console.log(), where you put the setup check’s lines in a file and run it with node.

Become a Member to join the conversation.