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
nodewith no arguments to open the REPL, the waypythonopens 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, thenundefined, becauseconsole.log()returnsundefined, JavaScript’s word for no value. Python’sprint()returnsNonetoo, but Python’s REPL doesn’t echoNone. A script never echoes, so theundefinednever reaches the report. - The up arrow brings back your last line. The REPL’s own commands start with a dot:
.helplists them, and.exitleaves. Ctrl+D leaves too. node -p "expression"runs one expression from the shell and prints its value, wherepython -cprints only what youprint().
Here’s the session from the video, after the banner:
> "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:
$ 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.