Functions we wish JavaScript shipped with by default

By "we" in the title, I mean @chilas and myself. We've been working with JavaScript a lot lately, and have had to create helper functions to ... well ... help us with our work.

javascript minions

Why can't we just call functions, minions and get on with it

So we compiled a list of these minions, and we hope that they are of help to you, just as they've been for us.

PS: ES<=5 fans, please forgive the ES6 syntax ... It's supposed to be the new cool.

String.isNullOrEmpty
String.isNullOrEmpty = (str) => str == null || str == "";  

this little guy simply checks that a string is null or empty. If you ask why, then you've probably never had to check this a lot.

Not the same thing

Number.isNumber
Number.isNumber = (num) => typeof(num) == "number"; //typed  
//or
Number.isNumber = (num) => !Number.isNaN(Number(num)); //type-free  

Wait! But we already have Number.isNaN, don't we? Why do we need this instead?
It's about the poor souls like us who can't follow logic chains in without getting confused. There's a threshold for number of !(not) symbols that we can understand at a glance.

It's JavaScript, not the rosetta stone

Number.prototype.round
Number.prototype.round = function (radix) {  
    return Number(this.toFixed(Number(radix) || 0));
}

This returns a round-up/down approximation of a Number. Yes, chill ... we know! You should hardly ever touch prototypes. But why this didn't come with JS by default, we don't know. Like why does Number.prototype.toFixed even return a string?

So, that's all folks! Let the JS purists come with their pitchforks 😜 We have your time!!!

Show Comments