Monday, January 26, 2015 at 1:51 PM

JavaScript type coercion fail

If you're not into JavaScript, please ignore.

I just spent an hour learning that this doesn't work in JavaScript:

var x = 12; alert (x.toLowerCase ());

Instead you have to say:

var x = 12; alert (x.toString ().toLowerCase ());

Why?

This seems counter to the philosophy of JavaScript, which strives to make sense of everything, no matter how convoluted. In this case it's fairly obvious what I wanted to do.

Only in very special circumstances in the code I'm working on is x a number. And when it is, converting it to lowercase, which should be a no-op, fails.

It doesn't help that Chrome's debugger is totally borked, and I had to resort to println debugging, which I used to do before there were source debuggers.

Can't fix

BTW, I would never advocate changing the behavior. Too deep, huge breakage potential.


Last built: Mon, Aug 24, 2015 at 9:08 AM

By Dave Winer, Monday, January 26, 2015 at 1:51 PM. Only steal from the best.