Reputation: 10755
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
Upvotes: 10
Views: 7741
Reputation: 56467
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.
Upvotes: 17