问题
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?
回答1:
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.
回答2:
Actually there's nothing in common between them.
return
- returns execution to the caller with optional result
console.log()
- logs out information in console.
回答3:
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.
回答4:
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
回答5:
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.
回答6:
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.
回答7:
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.
来源:https://stackoverflow.com/questions/21020608/difference-between-console-log-and-return-in-javascript