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

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Jan 28, 2013 at 11:53 user2018036user2018036 1311 gold badge1 silver badge3 bronze badges 1
  • 1 possible duplicate of What is console.log? – John Dvorak Commented Jan 28, 2013 at 11:54
Add a comment  | 

5 Answers 5

Reset to default 17

The 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