1.

What is the use of underscore variable in REPL?

Answer»

Underscore variable is a special kind of variable which USUALLY stores the value of its last execution result. Whenever you need to use the OUTPUT of one MODULE to the input of next module you may use this variable. It serves asimilar purpose as $? in bash. Please find below an example for a more detailed explanation:-  

Use _ to get the last result. 

C:Nodejs_WorkSpace>node 

> var x = 10  undefined  > var y = 20  undefined  > x + y  30  > var sum = _  undefined  > console.log(sum)  30  undefined  >

In Version 6.x or higher Underscore variable gives you the following result:- 

> [ 'a', 'b', 'c' ]   [ 'a', 'b', 'c' ]   > _.LENGTH 3   > _ += 1  

EXPRESSION assignment to _ now disabled.  

4   > 1 + 1   2   > _   4

In older version you will get a different result:- 

> [ 'a', 'b', 'c' ]  [ 'a', 'b', 'c' ]  > _.length  3  > _ += 1  4  > 1 + 1  2  > _  2


Discussion

No Comment Found