Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Functions Return Values

Dmitri Andronik
Dmitri Andronik
4,182 Points

Hello, i can't understand what's the difference between console.log() and return?

Hello, i can't understand what's the difference between console.log() and return?

4 Answers

Alex Devero
Alex Devero
24,526 Points

Hi Dmitri. There are couple of differences between console.log() and return. First is that return will not appear in console. Second and more important is that when you use return it will end loop or function, so the code below will not be executed. Console.log() will never do this.

Dmitri Andronik
Dmitri Andronik
4,182 Points

thanks,i got it,but what's the difference when i use them inside a function for example:

function square(x) { console.log(x*x); }

function square(z) { return z*z; }

see,what's the difference,because when i call the function,and i put a number inside of it,they both give me a correct answer.

Alex Devero
Alex Devero
24,526 Points

Console.log will display the parameter in the console. Return statement will stop executing of the function and return the specified value (not into the console).

Sam Matthews
Sam Matthews
8,276 Points

RETURN will also allow you to use the value returned in some other part of your program.

So myValue = mathFunctionAddition(2,2); Now you can go ahead and do whatever you want with myValue.

console.log is just a print statement, all languages have something similar. With console.log, you can't go ahead and do something with the value you print.

Thank you Alex and Sam. Clearest explanations on difference between return and console.log() I've encountered. O_o

Alex Devero
Alex Devero
24,526 Points

You are welcome Jay :-)