0

What are the instances when you would use console.log and return in javascript?

I've just started learning javascript and I want to know what are some instances when I'd use them?

Ronan
  • 329
  • 2
  • 7
  • 19
  • `console.log` is only used for debugging/informative purposes. `return` is an integral part of the Javascript language – CodingIntrigue Jan 09 '14 at 12:45
  • `console.log` is the equivalent of `print` in other languages. `return` is used for returning values from a function. – Amal Antony Jan 09 '14 at 12:48

7 Answers7

5

Actually there's nothing in common between them.

return - returns execution to the caller with optional result

console.log() - logs out information in console.

lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81
  • console.log is a function in javascript and return is a keyword. console.log is used to debug/print information to the console. Return on the other hand is a call to pass the value where the call was made console.log is not going to stop the execution while return stops the execution and return the specific value – surender pal Jun 28 '22 at 11:37
3

From http://blogs.msdn.com/b/cdndevs/archive/2011/05/26/console-log-say-goodbye-to-javascript-alerts-for-debugging.aspx

console.log will display the parameter passed to the log method in the console window. Use this method to display a string or variable in the console window.

You can use the console class in your code as well, much like we can use JavaScript alerts.

<script type = "text/javascript"> 
    function tryConsole() { 
        console.log("hello world"); 
    } 
</script>

When using the return statement, the function will stop executing, and return the specified value.

Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
1

Console.log emits a message to your browsers Console, and is usually used to emit debugging messages (research your browser's developer tools)

return is a keyword that terminates a function and possibly returns a value to the caller of that function.

Mister Epic
  • 16,295
  • 13
  • 76
  • 147
0

console.log prints the string in firebug console. Just found the below link, you can refer What is console.log and how do I use it?

and

return just return from the function without processing further code or you can say, A function immediately stops at the point where return is called.

Community
  • 1
  • 1
A Paul
  • 8,113
  • 3
  • 31
  • 61
0

They're for completely different purposes, console.log will return the value to the browser's console if it's supported, return will return a value from the javascript function

Sam Jones
  • 4,443
  • 2
  • 40
  • 45
0

console.log will not influence the flow of your code. Return values will cause reactions in your script, it really matters if you return false or true or whatever. For debugging I strongly suggest using console.log.

Ben
  • 391
  • 5
  • 13
0

Inside a clousure the return will return some value, and console.log will log some value in browser console. Console.log is like a debug tool for not good browsers like IE.

thiago.lenz
  • 399
  • 4
  • 11