Difference between return and console.log


#1

Dear programmers,

Can someone explain to me what the difference is between a return keyword and a console.log? They say console.log prints stuff out and return returns a value. Doesn't it do exactly the same?


#2

console.log() actually logs something to the console screen.
return just returns that value.

if you had a function that just logged, then you couldn't take the output of that function for the rest of the code.
if you had a function that returned, then you could get what the function "returns" and set it to a variable or use it otherwise

for example:

var testFunction = function(x){
    return x * 2
}

console.log(testFunction(2);
var multipliedByTwo = testFunction(2);

#3

What tekkie said was a good explanation

When you think about "return" don't think of it as printing a result out like console.log() does, but rather that return is something else entirely that lets you use what the function returns.

Hope that helps!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.