Logo
27 Oct 2014 | 1 min. (119 words)

Preventing console.log issues in IE

In some older browsers, including versions of Internet Explorer, window.console doesn’t exist and therefore calling console.log() will result in an error.

One way of fixing this is to create a function which checks whether console exists and use this function instead:

function consolelog(v){
    if (window.console
        && window.console.log){
        window.console.log(v);
    }
}

However this requires all developers knowing about this function, and if someone forgets and directly calls console.log() then older browsers will still throw an error.

Therefore a better solution is to check whether console.log() exists, and if not, create an empty function which prevents the error occurring.

if (!(window.console
    && window.console.log)){
    window.console = {
        log:function(v){return;}
    }
}

Alternatively a better shorthand method would be:

window.console = window.console || {log:function(a){}};
javascript console-log ie logging
Twitter Facebook

Using @font-face and preventing faux-styles

…

Round to the nearest decimal

…

Related Links

  • LinkedIn
  • Twitter: @curtcode
  • Stack Overflow
  • GitHub: @curtiscde

Stack Exchange

profile for Curtis on Stack Exchange, a network of free, community-driven Q&A sites
Follow @curtiscde

Recent Posts

  • My terminal setup and commands I use on a daily basis when working with GitHub
  • Displaying latest posts on your GitHub profile
  • Using Codecov within a monorepo
  • Displaying Strava stats using webhooks & GitHub Actions
  • Filtering duplicate data points on Chart.js
Theme Bleak by zutrinken Published with Hugo
Menu