Node.js – why undefined in node.js hello world example after console.log

node.js

i just started with node.js . So for starting with Hello World example node.js beginner book . when i type the command console.log("Hello world"); . Its prints Hello World on console . That what i expect from this code but in next line its also prints the undefined

i install the nodejs from this link for windows installation of nodejs for windows

here below is screenshot also for this

enter image description here

Best Answer

Every JavaScript function returns something. When you define function like this:

function test() {
    var x = 1;
}

then test() returns undefined. The same applies to the console.log function. What it does is that it passes the arguments to the display. And that's all. Thus it returns undefined.

Now Node JS shell works in such a way that whenever you input a function, variable, etc. it displays the value it returned. Thus console.log('Hello world!'); passes Hello world! to the screen and returns undefined, which then Node JS shell displays.

That's more or less how it works.