Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have seen code like this in several places: (function() { var method; var noop

ID: 646616 • Letter: I

Question

I have seen code like this in several places:

(function() {
    var method;
    var noop = function noop() {};
    var methods = [
        'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
        'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
        'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
        'timeStamp', 'trace', 'warn'
    ];
    var length = methods.length;
    var console = (window.console = window.console || {});

    while (length--) {
        method = methods[length];

        // Only stub undefined methods.
        if (!console[method]) {
            console[method] = noop;
        }
    }
}());
Specifically I am interested in this line of code

var console = (window.console = window.console || {});
Why are we creating a new window.console object if window.console is not defined ?

So why are we setting window.console={}?

Explanation / Answer

This creates a dummy windows.console for older browsers to prevent them from throwing errors. First an empty function is created and then the missing function calls are added (without any real functionality).