YouTip LogoYouTip

Nodejs Repl

Node.js provides a built-in REPL (Read-Eval-Print Loop), which is an interactive programming environment that allows you to run JavaScript code in the terminal. The name REPL comes from its main operations: Read, Eval, Print, and Loop. Node comes with an interactive interpreter that can perform the following tasks: * **Read** - Reads user input, parses the input Javascript data structure, and stores it in memory. * **Eval** - Evaluates the input data structure. * **Print** - Prints the results. * **Loop** - Loops through the above steps until the user presses **ctrl-c** twice to quit. REPL allows you to directly input and immediately execute JavaScript code, making it quick to verify code snippets. REPL is suitable for testing simple logic and debugging, as well as trying out new syntax or Node.js APIs. Start Learning REPL We can enter the following command to start the Node terminal: node After execution, the following content appears: # nodeWelcome to Node.js v20.1.0.Type ".help" for more information.> At this point, we can enter a simple expression after the > and press Enter to calculate the result. ### Simple Expression Operations Next, let's perform simple math operations in the Node.js REPL command line window: $ node > 1 +45> 5 / 22.5> 3 * 618> 4 - 13> 1 + ( 2 * 3 ) - 43> ### Using Variables You can store data in variables and use it whenever you need. Variable declaration requires the **var** keyword; if the var keyword is not used, the variable will be printed directly. Variables declared with the **var** keyword can be output using console.log(). $ node > x = 1010> var y = 10undefined> x + y 20> console.log("Hello World")Hello Worldundefined> console.log("example.com") example.com undefined ### Multiline Expressions Node REPL supports entering multiline expressions, which is somewhat similar to JavaScript. Next, let's execute a do-while loop: $ node > var x = 0undefined> do {... x++;... console.log("x: " + x);... } while ( x The **...** three-dot symbol is automatically generated by the system; just press Enter for a new line. Node will automatically detect whether it is a continuous expression. ### Underscore (_) Variable You can use the underscore (_) to get the result of the previous expression: $ node > var x = 10undefined> var y = 20undefined> x + y 30> var sum = _ undefined> console.log(sum)30undefined> * * * ## REPL Commands Summary table of common commands and shortcuts for Node.js REPL: | Command/Shortcut | Description | | --- | --- | | `.help` | Displays all available commands in the REPL and their descriptions. | | `.exit` | Exits the REPL session, equivalent to pressing `Ctrl + D`. | | `.save ` | Saves the current REPL session to the specified file. | | `.load ` | Loads and executes code from the specified file into the REPL. | | `.break` | Exits multiline expression input mode and returns to single-line input mode. | | `.clear` | Resets the REPL context, equivalent to clearing all variables and states. | | `Ctrl + C` | Forces exit of the current input or terminates the command. If pressed twice, it exits the REPL session. | | `Ctrl + D` | Ends the REPL session, equivalent to `.exit`. | | `Ctrl + L` | Clears the screen, similar to typing `clear` in the terminal. | | `Arrow keys (↑/↓)` | Browses input history, viewing and re-executing previously entered commands. | | `Tab` | Autocompletes input, displaying possible options or completing commands. | | `_` | Used to access the result of the last expression. | | `Ctrl + R` | Enters reverse search history to search for previously entered commands. | | `Ctrl + U` | Deletes all content from the cursor to the beginning of the line. | | `Ctrl + K` | Deletes all content from the cursor to the end of the line. | | `Ctrl + A` | Moves the cursor to the beginning of the line. | | `Ctrl + E` | Moves the cursor to the end of the line. | | `Ctrl + B` | Moves the cursor backward one character. | | `Ctrl + F` | Moves the cursor forward one character. | | `Ctrl + N` | Shows the next history entry (same as the down arrow key ↓). | | `Ctrl + P` | Shows the previous history entry (same as the up arrow key ↑). | | `Ctrl + Z` | Suspends the REPL session, putting it in the background (available on some systems). | * * * ## Stopping REPL As we mentioned earlier, pressing **ctrl + c** twice will exit the REPL: $ node >(^C again to quit)> ## Advanced REPL Features * **Autocompletion**: Type part of the code and press `Tab`, and the REPL will attempt to complete it or display possible options. * **Result Caching**: The REPL automatically saves the result of the last run into the special variable `_`. For example:> 5 + 510> _ * 220 * * * ## Gif Demo Next, we will demonstrate the example operations through a Gif image: !(#)
← Nodejs Event LoopPhp Filter Advanced β†’