3 ms·
I don't like abusing String prototype but: String.prototype.toInt=function(){ return parseInt(this,10) } then: "08".toInt() // ==> 8
by Kilimanjaro 16y ago
I don't like abusing String prototype but:
String.prototype.toInt=function(){ return parseInt(this,10) }
then:
"08".toInt() // ==> 8
- jherdman 16y agoHmm... I think you might find this a little more flexible in the long run: String.prototype.toInt = function (base) { if (typeof base == 'undefined') base = 10; return parseInt(this, base); } Smart defaults are always best, but there's no real need to handicap the users of your API.