Javascript String left(count)

Description

Javascript String left(count)



String.prototype.left = function(count) {
    var result = "";

    var stringLength = this.length;

    if (count >= stringLength || count < 0) {
        //http://stackoverflow.com/questions/5146591/javascript-wtf-a-string-prototypes-this-doesnt-return-a-string
        return String(this);
    }//ww  w.  j ava  2  s . c  om

    var i;
    for (i = 0; i < count; i += 1) {
        result += this[i];
    }

    return result;
};



PreviousNext

Related