Stubbing out the console object so Firebug statements can be left in place

22. July 2009 10:52

Okay, so you love the Firebug Console, and related output to the console tab within Firebug.  Don't we all, however there is a minor problem in leaving in all your log, and debug (etc) statements, and that is the fact that the console object doesn't exist in other browsers, or Firefox without Firebug.  You will want to put the script segments below into a file that is called before other scripts on your page.

First, I create a simple stub function off of the existing Function class, this allows for a single point of reference for empty functions, without generating a new Function instance for each method it gets used as.

	//stub out firebug console object
	//		will allow console statements to be left in place
	if (typeof Function.empty != 'function') Function.empty = function(){};

	if (typeof console == 'undefined') console = {};

	if (typeof console.log == 'undefined') console.log = Function.empty;

	if (typeof console.debug == 'undefined') console.debug = log;

	if (typeof == 'undefined') = log;

	if (typeof console.warn == 'undefined') console.warn = log;

	if (typeof console.error == 'undefined') console.error = log;

	if (typeof console.assert == 'undefined') console.assert = function(){
		var args =;
		var parm = args.shift();
		if (!parm) {
			throw new Error("Assert failed.");

	if (typeof console.dir == 'undefined') console.dir = function(input) { 
		if (typeof JSON != 'undefined' && typeof JSON.stringify == "function")
			console.log( JSON.stringify(input) );

	if (typeof console.dirxml == 'undefined') console.dirxml = console.dir;

	if (typeof console.trace == 'undefined') console.trace = Function.empty;
	if (typeof == 'undefined') = Function.empty;
	if (typeof console.groupCollapsed == 'undefined') console.groupCollapsed = Function.empty;
	if (typeof console.groupEnd == 'undefined') console.groupEnd = Function.empty;
	if (typeof console.time == 'undefined') console.time = Function.empty;
	if (typeof console.timeEnd == 'undefined') console.timeEnd = Function.empty;
	if (typeof console.profile == 'undefined') console.profile = Function.empty;
	if (typeof console.profileEnd == 'undefined') console.profileEnd = Function.empty;
	if (typeof console.count == 'undefined') console.count = Function.empty;

	function log() {
		if (typeof JSON != "undefined" && typeof JSON.stringify == "function") return console.log(JSON.stringify(arguments));
		return console.log(arguments.toString());

Updated 2011-12-16: I extended this to include a bit more functionality for those browsers with console.log, but not much else.

Comments are closed


Michael J. Ryan aka Tracker1

My name is Michael J. Ryan and I've been developing web based applications since the mid 90's.

I am an advanced Web UX developer with a near expert knowledge of JavaScript.