Nodejs String Starts With startsWith(substring)

Here you can find the source of startsWith(substring)

Method Source Code

"use strict";//from   www.j a v  a  2  s.  c  o m

String.prototype.startsWith  = function(substring) {
    var substringLength = substring.length;

    if (substringLength > this.length) {
        return false;
    }
    var i;
    for (i = 0; i < substringLength; i += 1) {
        if (substring[i] !== this[i]) {
            return false;
        }
    }

    return true;
};

Related

  1. startsWith(string)
    String.prototype.startsWith = function(string){
      if (typeof(string) !== "string") {
        throw new Error("Input argument was not a string");
      } else if (this.substring(0, string.length) === string) {
        return true;
      } else {
        return false;
    };
    ...
    
  2. startsWith(string)
    String.prototype.startsWith = function (string){
      return this.indexOf(string) === 0;
    };
    
  3. startsWith(substr)
    String.prototype.startsWith = function(substr) {
        for (var i = 0; i < substr.length; i++) {
            if (this[i] !== substr[i]) {
                return false;
        return true;
    
  4. startsWith(substr)
    String.prototype.startsWith = function(substr) {
      return (this.indexOf(substr) == 0);
    };
    
  5. startsWith(substring)
    String.prototype.startsWith = function (substring) {
        var stringCut = this.substring(0, substring.length);
        return stringCut === substring;
    
  6. startsWith(substring)
    String.prototype.startsWith = function(substring) {
        var compareString = this.substring(0, substring.length);
        return compareString === substring;
    };
    
  7. startsWith(suffix)
    String.prototype.startsWith = function(prefix) {
      return (this.indexOf(prefix) == 0);
    };
    module.exports = {};
    
  8. startsWith(suffix)
    String.prototype.startsWith = function (suffix) {
        return !(this.indexOf(suffix) !== 0);
    };
    
  9. startsWith(text)
    console.log(typeof Array.prototype.sort);          
    console.log(typeof String.prototype.substring);    
    String.prototype.startsWith = function (text) {
      return this.indexOf(text) === 0;
    };
    var msg = 'Hello world!';
    console.log(msg.startsWith('Hello'));