Having them available allows us to extend them. For example, if the browsers don't implement it, you can add the Array.isArray
method yourself.
Array.isArray = function( arr ) {
return Object.prototype.toString.apply( arr ) === '[object Array]';
};
This is good to have for most polyfills.
Another nice example is the trim
method on strings.
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,'');
};
Otherwise, there isn't really other uses. Converting to a number is better done with +
, converting to a string is better done with '' +
, etc.
The only one I regularly use is Regexp
though, as it's convenient to do new Regexp
(for saving in a variable, because less escaping is needed or because it allows to break up the regex in multiple lines).
Edit: I just thought about another use:
arr.filter( Boolean ); // Removes falsy values from the array "arr"
arr.map( Number ); // Converts every element of the array to a Number