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

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

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 In this lesson, you’ll open Node’s REPL, try a line of the setup check in it, and leave it again. Before porting a script, you often try a line at a prompt first.

00:13 In Python, you start the REPL by running python with no arguments. Type the first of the team’s next steps, and the REPL echoes its value back.

00:25 Node comes with a REPL too. Run node with no arguments, the way you ran python. It greets you with its version and a hint, then waits at its own prompt: one angle bracket instead of three.

00:40 Type the same step. Node’s REPL echoes it back, in single quotes, the way Python’s does. Trying a line works the same in both.

00:52 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.

01:06 Type the same step with it. What will Node’s REPL show? Your turn: pause the video and predict.

01:19 Two lines: the step, and then undefined.

01:26 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:46 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:57 In a script, nothing is echoed, so the report gets the step and nothing else.

02:05 The up arrow brings back your last line, as it does in Python’s REPL, and Enter runs it again. The REPL also has its own commands, and they all start with a dot.

02:17 Type .help, and it lists them.

02:21 To leave, type .exit, or press Control D. Watch out for Python’s exit(). Node’s REPL doesn’t have it, and you’d stay at the prompt.

02:36 For one line, you don’t need the REPL at all. Python’s -c runs a line from the shell, and you print what you want to see. Node’s -p runs one expression and prints its value for you, like a one-line REPL.

02:52 Here, it asks Node’s process object for the operating system, the value the setup check reports. Both print linux.

03:03 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.

03:15 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.