Skip to content

Instantly share code, notes, and snippets.

@truetone
Created May 22, 2015 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save truetone/c2c8c121dbfc26e776f9 to your computer and use it in GitHub Desktop.
Save truetone/c2c8c121dbfc26e776f9 to your computer and use it in GitHub Desktop.
Handy JavaScript Functions
/**
* JavaScript's typeof method returns "object" for arrays and null values. This fixes that.
* http://javascript.crockford.com/remedial.html
*/
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (value instanceof Array) {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
}
/**
* Evaluates any JavaScript object that is a Number to see if it is a
* multiple of the number passed
* @example
* // returns true
* 15.isMultipleOf(5);
* @method
* @param {Number}
* @returns {Boolean}
*/
Number.prototype.isMultipleOf = function(n) {
return (this % n) === 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment