Console.log vs. return


#1

When should I use console.log and when return? And what's the difference anyway? Just wondering, but it's soooooooo confusing. :scream:


#2

Day 3 of being confused here...


#3

return is used to get the out come of something (i.e function)
console.log is used in to get the out put of a statement

:wink:


#4

Does it affect the code, or is it just a rule that helps with organization or something? Like, if I typed:
return "Hi, everybody!"
would it work the same as if I had typed:
console.log("Hi, everybody!");
or would it actually change the way the code works?


#5

Return is to put the outcome like the code

stuf = false;
if(stuf = true){
return true;
}
if there was an while statement after the code I put in like this:

while(true){
console.log("stuf");
stuf = false;
}


#6

Ohhhh.... Now I get it. Thx :smile:
jrpsychgal


#7

Just one question, does it actually affect the code which one you use?


#8

Does anyone know if it actually affects the code? They both print things, and I understand when to use each one, but still does it change the way the code works if you use one or the other?


#9

I'm not a javascript expert, but I'm a full-time programmer in other languages. I'm pretty sure that there's a really big difference between return and console.log. Return is what the function "returns" to the software that called it. Console.log prints to the console, which is useful for troubleshooting, but doesn't really do anything you can use in software.

To be more concrete: imagine that I want to write a utility that communicates user data to a server. As part of that utility, imagine that I write a function called UserString() that takes an object full of user information, and parse it into a long string. (Note that UserString() is not the entire program, it's just one part of it. Other functions would retrieve the user data and initiate contact with the server.) If I used console.log inside of UserString() to present the resulting string, it would only get printed to the console, which wouldn't be helpful to anyone. We would need to use "return" inside UserString() so that the function that calls UserString() will be able to use the string.

(Hopefully this is right. Again, this is based on my understanding of other languages. I'm no web expert.)


#10

Thanks so much! I really get it now. :slight_smile: