admin管理员组文章数量:1180451
if (open_date) {
open_date = get_date_from_string(open_date);
window.console && console.log(open_date);
window.console && console.log(cancel_until);
What is window.console && console.log
? Does it have to be in the code? Through this script does not work on IE (all version) --> IE runs javascript only after pressing F12
if (open_date) {
open_date = get_date_from_string(open_date);
window.console && console.log(open_date);
window.console && console.log(cancel_until);
What is window.console && console.log
? Does it have to be in the code? Through this script does not work on IE (all version) --> IE runs javascript only after pressing F12
- 1 possible duplicate of What is console.log? – John Dvorak Commented Jan 28, 2013 at 11:54
5 Answers
Reset to default 17The rightside-expression will only get evaluated if the leftside-expression is truthy
. Thats how the logical AND operator works.
Its basically short for
if( window.console ) {
console.log( open_date );
}
As you might guess correctly, it's a common pattern for this case, because the console
object might not be available on every browser (especially mobiles).
1.) What is window.console && console.log ?
console.log refers to the console object used for debugging. for firefox i use firebug for example.
but if the console is not available the script will crash. so window.console checks if the console object is there and if so it uses its log function to print out some debug information.
2.) Does it have to be in the code?
no, its only for debugging purpose
window.console && console.log(open_date);
The above code is just short for an if conditional statement. It doesn't have to be there. it is for debugging purposes. For most browsers, you can hit F-12 to open the browser debug console. Chrome has a debug console built in. Firefox has an extension called FireBug that you can use. Below is the equivalent statement without the '&&'.
if (window.console) console.log(open_date);
I prefer to add the following code at the beginning of my javascript code so that I do not have to have these "if" statements all over the place. It really saves space and removes potential errors.
if (typeof console == "undefined") { window.console = {log: function() {}}; }
Jon Dvorak's comment above contains an elegant alternative way of doing this:
console = window.console || {log:function(){}}
Console.log is a logger for browser which logs the messages on browser console. EDIT: Console.log is not supported for lower versions of Internet Explorer
This condition is used to prevent errors on IE ... because, unfortunately, in IE (version 8) we cannot use console.log("") .... however testers still view the logs on Chrome ...
本文标签: javascriptWhat is windowconsole ampamp consolelogStack Overflow
版权声明:本文标题:javascript - What is window.console && console.log? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738119530a2064827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论