3

I need to distinguish "0" from 0 in Firebug console.log output.

If you type console.log("0") into Firebug console, you'll get 0, same as if you'd typed console.log(0), but I need it to be "0".

In other words, expected output:

console.log("0")
> "0"
console.log(0)
> 0

Actual:

console.log("0")
> 0
console.log(0)
> 0

What is the best solution to this problem?

I've found some workarounds like console.log("%o", "0") or console.log(["0"]), but they are too clumsy and obtrusive to use them everywhere.

I've also tried debug and info without success.

By the way, Chrome console gets it right, but I'm not ready to move to Chrome yet.

I'm thinking about writing some wrapper around console object. I'm not sure how to do it right but I'll try it if there is no other solution.


So, the only solution seems to be that you need make some wrapper that looks like this (based on @wsanville's answer):

var log_orig = console.log;
// No need to check for type, just use %o for everything
// Also, it gets messier for multiple arguments
console.log = function(s) { log_orig('%o', s); }

But then you get the problem with line numbers, as described in How to access line numbers when wrapping Firebug (or similar) Console api

I guess I should file a firebug bug or something, because this can be really critical in some situations.

Community
  • 1
  • 1
Valentin Nemcev
  • 4,940
  • 2
  • 20
  • 21

3 Answers3

3

You could try using console.log(new String('0')) to distinguish from the two cases.

You could also just clobber console.log and encapsulate whichever solution you like best, like this:

var log_orig = console.log;
console.log = function(s)
{
    if (s === '0')
        log_orig('%o', s);
    else
        log_orig(s);
}

console.log('0'); //results in "0"
wsanville
  • 37,158
  • 8
  • 76
  • 101
1

You could use:

console.log("Variable x type is " + typeof(x))

The typeof unary operator determines the type of the variable.

The return values are boolean, number, object, string, or undefined.

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
1

console.log(0) will show 0 but in darkblue color console.log('0') will show 0 in black color

Also you could redefine console.log function:

var _log = console.log;
console.log = function(x) {
    if ( Object.prototype.toString.call(x).indexOf('String') != -1 ) {
        return _log("'"+x+"'");
    }  else {
        return _log(x);
    }
}
Larry Foobar
  • 11,092
  • 15
  • 56
  • 89