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?
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?
Actually there's nothing in common between them.
return
- returns execution to the caller with optional result
console.log()
- logs out information in console.
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.
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.
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.
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
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.
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.