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
The same two lines in Python’s REPL show one line less, since Python doesn’t echo None:
>>> "request VPN access"
'request VPN access'
>>> print("1. request VPN access")
1. request VPN access
Python’s exit() doesn’t work in Node’s REPL. Node doesn’t have it, so you get an error and stay
at the prompt:
> exit()
Uncaught ReferenceError: exit is not defined
Type .exit or press Ctrl+D instead. For a single value, skip the REPL and use node -p:
$ node -p "process.platform"
darwin
The value is your operating system, the one the setup check reports: darwin on macOS. Linux
prints linux, and Windows prints win32.
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.