0

If there is any case where console.log is undefined, I would need to define it or wrap my logging calls in my own log function.

Is there any javascript runtime environment where console.log could be undefined? By runtime environment I mean a browser or other environments like Node.

Arne Evertsson
  • 18,823
  • 18
  • 64
  • 82

1 Answers1

2

In IE9-, console.log only exists when the developer console is openned.

If you need to logs in those browsers, it may be a good idea to deactivate console or to test before logging.

To make console.log works on those old browsers, a tricky but smart way is to set at the start of your JS this code:

if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function() {};

Credits to Michael Erickson


For further details, here is a very complete blog about cross-browser compatibility.

Community
  • 1
  • 1
Mistalis
  • 16,351
  • 13
  • 68
  • 91