DEV Community

Johan H. Guzman Gil
Johan H. Guzman Gil

Posted on

Node.js - REPL (Read Eval Print Loop)

Before start with this post, I would like to explain what is node.js.

Node.js is not entirely true a JavaScript that runs on the backend, it is a wrapper of a VM who is running the JavaScript V8 engine,
knowing that, node.js come with a lot API that we can use in conjunction with JavaScript but I will leave that topic for another post.

Now let talk about REPL.

REPL it is a node API that let us to write or test script in the terminal.
To enter in to the REPL mode we need to type node in the terminal.

> node
>
Enter fullscreen mode Exit fullscreen mode

After that are going to see an empty > its mean that we are into the REPL mode, now we can have fun and write some code.

> console.log('hello REPL') // => hello REPL
Enter fullscreen mode Exit fullscreen mode

REPL let us write an inline code by default, if we want to write a multiple line of code

> function printHelloAliens() {
Enter fullscreen mode Exit fullscreen mode

then left { open and press enter you will see the next line with ...

> function printHelloAliens() {
> ...
Enter fullscreen mode Exit fullscreen mode

it means that we are ready to continue with the function content, add a console.log('hello aliens') and press enter and you will see the next line with ...

> function printHelloAliens() {
> ...console.log('hello aliens');
> ...
Enter fullscreen mode Exit fullscreen mode

to finish with the function content with need to add }

> function printHelloAliens() {
> ...console.log('hello aliens');
> ...}
> undefined
Enter fullscreen mode Exit fullscreen mode

after press enter the terminal will print undefined;
if you want to test your function just invoke the function, in my case is printHelloAliens()

> function printHelloAliens() {
> ...console.log('hello aliens');
> ...}
> undefined
> printHelloAliens() // => hello aliens
Enter fullscreen mode Exit fullscreen mode

they are a lot of options, if you want to know it just type .help

> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Enter fullscreen mode Exit fullscreen mode

for this tutorial we're only going to see the .editor command.
The .editor command allow you to write a multiple line of code more easy.

> .editor
// Entering editor mode (^D to finish, ^C to cancel)
Enter fullscreen mode Exit fullscreen mode

Now with can write a multiple line of code with just pressing enter

// Entering editor mode (^D to finish, ^C to cancel)
function printMyName() {
 console.log('Johan Guzman')
}
> undefined
Enter fullscreen mode Exit fullscreen mode

For close the editor mode type CTRL + d and the terminal will print undefined;

if you want to test your function just invoke the function, in my case is
printMyName()

// Entering editor mode (^D to finish, ^C to cancel)
function printMyName() {
 console.log('Johan Guzman')
}
> undefined
> printMyName() // => Johan Guzman
Enter fullscreen mode Exit fullscreen mode

The REPL is very helpful to test a node or JavaScript code without open the editor or write a little program.

Top comments (1)

Collapse
 
manokero profile image
Wildin Mota

Great articule! is usefull to know that exist some options online like this one repl.it/languages/nodejs