I'm learning some of nodes core modules and i've wrote a little command line tool to test out the readline module but on my console.log() outputs, i'm also recieving undefined under it :/
Here's my code..
var rl = require('readline');
var prompts = rl.createInterface(process.stdin, process.stdout);
prompts.question("What is your favourite Star Wars movie? ", function (movie) {
var message = '';
if (movie = 1) {
message = console.log("Really!!?!?? Episode" + movie + " ??!?!!?!?!, Jar Jar Binks was a total dick!");
} else if (movie > 3) {
message = console.log("They were great movies!");
} else {
message = console.log("Get out...");
}
console.log(message);
prompts.close();
});
And here's what im seeing in my console..
What is your favourite Star Wars movie? 1
Really!!?!?? Episode1 ??!?!!?!?!, Jar Jar Binks was a total dick!
undefined
Why am I getting back undefined?
Why am I getting back
undefined?
Because console.log doesn't have a return value, so you're assigning undefined to message.
Since you're outputting message later, just remove the console.log calls from the lines where you're setting the message. E.g., change
message = console.log("Really!!?!?? Episode" + movie + " ??!?!!?!?!, Jar Jar Binks was a total dick!");
to
message = "Really!!?!?? Episode" + movie + " ??!?!!?!?!, Jar Jar Binks was a total dick!";
Side note: Your line
if (movie = 1) {
assigns the number 1 to movie and then tests the result (1) to see if it's truthy. So no matter what you type in, it will always take that branch. You probably meant:
if (movie == 1) {
...although I would recommend not relying on implicit type coercion of user-supplied input, and so I'd put this near the top of that callback:
movie = parseInt(movie, 10);
console.log doesn't return a value, so the result is undefined.
Note: comparison is done with ==, eg: movie == 1.