Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js displays "undefined" on the console

Recently I installed node.js on my Windows 7 machine.

On execution of JavaScript, I get an undefined message along with successful execution of the expression.

What's wrong here? I have not noticed any other side effects.

enter image description here

like image 674
Sandeep G B Avatar asked Dec 10 '11 15:12

Sandeep G B


People also ask

Why does node say undefined?

node also prints undefined because IN GENERAL, it displays the return value of each command and console. log doesn't return anything. Show activity on this post. this is happening because console.

Why does it say undefined in console?

This is because console. log() does not return a value (i.e. returns undefined). The result of whatever you entered to the console is first printed to the console, then a bit later the message from console. log reaches the console and is printed as well.

What is undefined in Nodejs?

The undefined property indicates that a variable has not been assigned a value, or not declared at all.


2 Answers

The JavaScript functions always return something. If you don't specify something to return in the function, 'undefined' is returned by default (you can check this out in Firebug too).

Don't worry though, this doesn't affect anything, you can ignore it.

like image 172
alessioalex Avatar answered Sep 20 '22 15:09

alessioalex


Just write "hello world"; and hit enter... it will return "hello world" instead of undefined, thus no undefined is displayed. console.log returns undefined and also logs arguments to console so you get multiple messages.

like image 28
Esailija Avatar answered Sep 20 '22 15:09

Esailija