Difference between console.log and return in javascript? [closed]

一世执手 提交于 2020-01-09 07:06:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!