Why isn't console.log the same as return?
Why a function "printed everything" or "computed everything" and still returns undefined: three reasons.
undefined. Who's right? Both. Let's sort it out.function addLog(a, b) {
console.log(a + b);
}
function addReturn(a, b) {
return a + b;
}
const x = addLog(2, 3);
const y = addReturn(2, 3);
console.log(x, y);console.log and return do completely different things:
console.logshows a value to a person in the console. The program doesn't receive it — it's like saying a number out loud.- return hands the value to whoever called the function. It can be stored in a variable, passed on, compared.
A function without return always returns undefined — even if there was a console.log inside.
Why is it designed this way? A function is a part of a program. Other parts of the program don't "read" the console: they get only what the function returned. The console is a window for the developer; return is the wire the result travels along.
console.log is for looking, return is for handing over. I, for one, don't show my fish — I return it to the bowl. To myself.console.log instead of return is the most common cause of a surprise undefined. But not the only one. There are two more, and both look as if return is right there.
Reason two: curly braces on an arrow. A short arrow (x) => x * 2 returns its expression by itself. Wrap the body in { }, though, and it becomes an ordinary function body. An ordinary body returns nothing until you write return.
const double1 = (x) => x * 2;
const double2 = (x) => {
x * 2;
};
const double3 = (x) => {
return x * 2;
};
console.log(double1(5), double2(5), double3(5));x * 2 and throws the result away.Why doesn't JavaScript return the last expression on its own, like some other languages do? Because a function body can have many lines, and the language won't guess which one you meant to hand back. Without braces there's only one line, so there's nothing to guess.
Reason three: `return` on a line of its own. JavaScript inserts semicolons at line ends where it thinks they belong. After a bare return it always inserts one. So return leaves alone, and the expression on the next line never runs.
function total(price, count) {
return
price * count;
}
function totalFixed(price, count) {
return price * count;
}
console.log(total(50, 3));
console.log(totalFixed(50, 3));return turns it into return;.You can split a long expression over lines — just start it on the same line as return. For example, open a parenthesis: return ( and then as many lines as you like.
Bonus: a function instead of a result. A function's name without parentheses is the function itself, the machine. With parentheses it's a call: the machine has already run, and you're holding its result. Mixing them up is a sure way to get a strange value.
function sayMeow() {
return "meow";
}
function shout(getWord) {
return getWord() + "!";
}
console.log(typeof sayMeow);
console.log(typeof sayMeow());
console.log(shout(sayMeow));shout expects a callback, a function it will call itself. It gets sayMeow, not sayMeow().undefined coming out of a function: is there a return? Is it on a line of its own? Does the arrow have curly braces? Three questions, and nine mysteries out of ten are solved.Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.