Blog

JavaScript Snippet – String.prototype.reverse()

The following is a quick JavaScript function which will return the reverse of a string: String.prototype.reverse = function() { return this.split("").reverse().join(""); }; The above function is a function that, once defined, will exist for every string. Here are some examples of how to use the above function: alert("Hello world!!!".reverse()); // Read more…

By Chris West, ago
Blog

JavaScript Snippet – String.prototype.contains()

The following is a quick JavaScript function which can determine if a string contains a substring: String.prototype.contains = function(str, ignoreCase) { return (ignoreCase ? this.toUpperCase() : this) .indexOf(ignoreCase ? str.toUpperCase() : str) >= 0; }; The above function is a function that, once defined, will exist for every string. The Read more…

By Chris West, ago
Blog

JavaScript Snippet – String.prototype.startsWith()

The following is a quick JavaScript function which can determine if a string starts with a substring: String.prototype.startsWith = function(str, ignoreCase) { return (ignoreCase ? this.toUpperCase() : this) .indexOf(ignoreCase ? str.toUpperCase() : str) == 0; }; The above function is a function that, once defined, will exist for every string. Read more…

By Chris West, ago
Blog

JavaScript Snippet – String.prototype.endsWith()

The following is a quick JavaScript function which can determine if a string ends with a substring: String.prototype.endsWith = function(str, ignoreCase) { return (ignoreCase ? this.toUpperCase() : this).slice(-str.length) == (ignoreCase ? str.toUpperCase() : str); }; The above function is a function that, once defined, will exist for every string. The Read more…

By Chris West, ago
Blog

JavaScript – Binding To A Function

ECMAScript 5 has introduced the new Function.prototype.bind() function which not only gives you the ability to natively bind a primitive or object to a function and also curry data for the function. Unfortunately, up to this point, nobody has written an implementation of this function to make it work on Read more…

By Chris West, ago
Blog

JavaScript – Add Commas To Numbers

At times, I have had to add commas to numbers in order to make them look better in different applications. Even though there isn’t a native way to do this in JavaScript, the code can be quite short, sweet and to the point: function addCommas(intNum) { return (intNum + '').replace(/(\d)(?=(\d{3})+$)/g, Read more…

By Chris West, ago