Signup/Sign In
LAST UPDATED: NOVEMBER 19, 2019

Node.js - REPL Terminal

    Node.js terminal is also known as REPL Terminal/Console and it is just like IDLE is the terminal for Python etc. The REPL terminal helps in testing simple Node.js code.

    The REPL stands for Read-Eval-Print-Loop where,

    • Read(R) - It reads the user's input and parses the input into JavaScript data structure and stores it into memory.

    • Eval(E) - The Eval takes the JavaScript data structure and evaluates it.

    • Print(P) - It prints the result of the evaluated data structure.

    • Loop(L) - Loops the command until the user presses Ctrl+c twice (it is used to exit from the REPL console).

    The REPL console is just like your console on chrome. You can open the console from the command prompt(on Windows) or terminal(on Mac or Linux) using the command node.

    C:\>node

    As the console is just like Chrome's console, you can test anything on it.

    For example, if you write 100+50 it will display the addition of 100 and 50 i.e. 150 in the new line.

    C:\>node
    > 100+50
    150
    >

    You can also use + operator to concatenate two strings.

    > "Hey" + " there!!"
    'Hey there!!'
    >

    Example of variables,

    Example of variables


    You can also use multiline expressions or functions by pressing Enter. The REPL terminal will display three dots (...), it means you can continue on the next line.

    > function addNum(x,y) {
    ... return x+y;
    ... }
    undefined
    > addNum(20,50)
    70
    >

    To execute an external JavaScript file by writing node filename command. For example, if there is a file test.js which prints on console "Hello World" then,

    C:\ node test.js
    Hello World
    >

    To exit from the REPL terminal, press Ctrl + C twice.

    >
    (To exit, press ^C again or type .exit)
    >
    C:\>



    Some other REPL commands

    REPL Command Description
    .help Display help on all the commands.
    tab Keys Display the list of all commands.
    Up/Down Keys See previous commands applied in REPL.
    .save filename To save the current Node REPL session to a file.
    .load filename To load the specified file in the current Node REPL session.
    ctrl + c Terminate the current command.
    ctrl + c (twice) To exit from the REPL.
    ctrl + d Terminate the Node REPL.
    .break To exit from multiline expression.
    .clear To exit from multiline expression.



    Conclusion

    REPL console/terminal is a great way to practice basics of Javascript/Node.js in general for example when you are learning about datatypes, operators etc you can use REPL console to run simple code examples and see the result.

    Working as an Application Developer. I love to learn and discover things JavaScript, HTML, and CSS.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS