Thursday 12 September 2013

Create a endsWith Function in JavaScript.

Javascript does not have a endsWith function so we have to create it manually we use String prototype for create endsWith function.
if (typeof String.prototype.endsWith !== 'function') {
    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };
}
Then you can use it directly on string values.
var data = "Lorem ipsum dolor sit amet";
data.endsWith('amet'); //true



No comments :

Post a Comment