Wednesday, 16 May 2018

004 Node.js REPL Terminal

REPL Terminal
Node.js have virtual environment called REPL or Node Shell ,REPL stands for Read Eval Print Loop.REPL is commonly used for debug JS code.
  • Read − Reads user's input, parses the input into JavaScript data-structure, and save in memory.
  • Eval − Evaluates the data structure.
  • Print − Prints the result.
  • Loop − Loops the above command until the user presses ctrl-c twice.

To open REPL , Open command prompt , then type "node".Below is the image




You can evaluate expression in REPL also.



Example 1 (Simple Expression)
10+20
=30

Output





















Example 2 (Complex Expression)
12+((11+3)*9)/3
=54


Output













Variable in REPL 
 
Variables are used to store values. var keyword is used for variable declaration.You can evaluate expression with variable also.



Example 1 (Simple Expression)
> var a=10
undefined
> var b=20
undefined
> a+b
30
Output :

















Multiline Expression  

Node.js REPL supports multiline expression similar to JavaScript.

Example 1 

for(var l=0;l<20 br="" l=""> <20 20="" l="">

<20 br="" l="">{
  console.log(l*l);
}

Output :

 

 


Node.js Underscore Variable 

You can also use underscore _ to get the last evaluated value.

Example 1 

> 1+9+8+6
24
> _
24

Output :

 

 











REPL Commands

help:The .help command is used to display all the  REPL commands.

Example

C:\Users\dbhattacharjee>node
> .help


Output :










exit :Exit the repl

Example 
C:\Users\dbhattacharjee>node
> .exit 

Output :
C:\Users\dbhattacharjee>



save :Save all evaluated commands in this REPL session to a file.

Example
C:\Users\dbhattacharjee>node
> 5+9+3
17
> .save myData.txt
Session saved to:myData.txt




load :Load JS from a file into the REPL session


Example 



C:\Users\dbhattacharjee>node
> 4+5+6+4
19
> .save myFile.txt
Session saved to:myFile.txt
> .load myFile.txt
4+5+6+4

19
>


editor : Enter editor mode
 
Example
C:\Users\dbhattacharjee>node
> .editor
// Entering editor mode (^D to finish, ^C to cancel)
console.log('hello world');
hello world
undefined
>

tab keys :list of current commands.
Example



 REPL Keyboard Commands

(a) ctrl + c :It is used to terminate the current command.
(b) ctrl + c twice :It terminates the node repl.
(c) ctrl + d :It terminates the node repl.
(d) up/down keys :It is used to see command history and modify previous commands.









No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...