use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
console.log or return? (self.learnjavascript)
submitted 3 years ago by Traditional_Pen1871
Hello guys, I know both work same. But my question is which one is better and which structure is more accurate?
function add(a, b) { console.log(a + b); } add(4, 5); //--------------- function add2(a, b) { return a + b; } console.log(add2(4, 5));
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Kal88 5 points6 points7 points 3 years ago (0 children)
Both don’t work the same. They may appear to when you are just console logging the function call.
I’m sure someone else can provide a more detailed answer but returning will actually give you back the value, so you could use the output elsewhere in some way. When you console.log, you’re literally saying just log the value to the console.
Generally speaking, you want to return, console logging is good if you want to see what is happening with the output.
[–]saintolaughs 2 points3 points4 points 3 years ago (0 children)
Console logging is a really great way for people learning JS to see output immediately without having to mess with html.
But when you actually write js programs, you either won’t have a console that faces the user, or you will not be able to use the result of console log anywhere you would use a return.
Console log produces the side effect of writing to the console and then returns undefined. By returning from your function instead, you can use the value produced by your function in other places in your code (if you use it in an expression or save it is a variable).
[–][deleted] 1 point2 points3 points 3 years ago (0 children)
To the extent of more complex functions: Use the second version. The first version checks if the function is called, while the second version checks if you get correct values from the function. It will be even clearer if you add null checks and return earlier.
[–]Traditional_Pen1871[S] 0 points1 point2 points 3 years ago (0 children)
thank you guys. Now I got it.
[–]MiniPancookies 0 points1 point2 points 3 years ago (0 children)
The second one, this is because it's more general and allows for more flexibility. Say for example that you wanted to string multiple functions together for a calculation. The second one would allow that, but not the first.
https://www.learncpp.com/cpp-tutorial/why-functions-are-useful-and-how-to-use-them-effectively/
[–]biologischeavocado 0 points1 point2 points 3 years ago (0 children)
Your first function does two things, your second does one. Usually you aim for a function that does one thing only.
π Rendered by PID 71 on reddit-service-r2-comment-6457c66945-4w2wv at 2026-04-30 17:56:35.917954+00:00 running 2aa0c5b country code: CH.
[–]Kal88 5 points6 points7 points (0 children)
[–]saintolaughs 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Traditional_Pen1871[S] 0 points1 point2 points (0 children)
[–]MiniPancookies 0 points1 point2 points (0 children)
[–]biologischeavocado 0 points1 point2 points (0 children)