Node JS undefined in REPL


Node JS REPL


What is undefined in  JavaScript?

according to https://developer.mozilla.org/en-US/docs/Glossary/Undefined  undefined is:

undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.

Also

An argument is a value (primitive or object) passed as input to a function.

Why NodeJs REPL prints undefined?

We know that REPL stand for READ, EVALUATE, PRINT and LOOP.  

So when we use a  NodeJS REPL, every line of code that we write is immediately processed or evaluated.  

Why NodeJs REPL prints undefined?

We know that REPL stand for READ, EVALUATE, PRINT and LOOP.  

So when we use a  NodeJS REPL, every line of code that we write is immediately processed or evaluated.  The line of code which return some value are printed as an output. 

When there is no output then undefined   is printed.

Consider the following code:

let mynum;  //create a variable but assign it no value so there.  

The above is a deceleration  of a variable.  The given piece of code does not return any value. 

> let mynum
undefined

Even if in the above code you assign a value it will still return undefined, as there is a decleration and assignment. The code does not return a value.

> let myname = "Dexter"
undefined

In REPL env the evaluated output of the statement is 'undefined' and that is what gets printed.

Similarly for:

console.log("Hello");

The above is a functions, functions are expected to return a value, here we do not receive a value so we see a 

> console.log("Hello")
Hello
undefined

What about  assignment, why does it print a value and not undefined?

The assignment (=) operator is used to assign a value to a variable or property. The assignment expression itself has a value, which is the assigned value.

So when we say  x=10  The expression value is 10 i.e. the assigned value. 

So when you do this in the REPL env 10 is printed.

> x=10
10

But when you do console.log(x), it will print value of x and undefined for the console.log functions.
> console.log(x)
10
undefined

Finally why a value of variable is printed directly if it is assigned?

A mentioned initially  REPL will evaluate every line of code immediately, so as soon as we type in only a variable name, REPL evaluates its value and prints the same.  If a variable is uninitialized it gives an error or uncaught reference.

lets declare a variable and then just evaluate the variable.

> let xy = 10
undefined

Returns undefined as it does not return any value but xy is assigned with a value if 10.

so if you just type in xy and press enter, REPL evaluates the variable and prints its value.

> xy
10


.EOT

 

 

 

 

 

 


 

Main category

Add new comment

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
CAPTCHA
Enter the characters shown in the image.
Are you a robot?
Address

OpenSourceCook.in
"Natraj"  Bungalow,
Colony No.7,  Sr.No. 38.
(Lane Behind Sai Baba Mandir)
Kale Borate Nagar, Hadapsar,
Pune - 411028.
Get Directions