Design & Download
Your JS Library
in Seconds!!!

Snippet: startsWith() & endsWith()

Written by Christopher West (cwest) on July 26, 2016.
Determines if a specific target is at the beginning or end of an array or a string.
function startsWith(subject, target, opt_startAt) {
  var subjectType = typeOf(subject), subjectIsArray = subjectType == 'Array';
  if (subjectIsArray || subjectType == 'String') {
    subject = subject.slice(opt_startAt);
    if (subjectIsArray) {
      target = toArray(target);
      for (
        var tLen = target.length, i = tLen > subject.length ? -2 : tLen;
        i-- > 0 && subject[i] === target[i];
      );
      return i == -1;
    }
    return subject.indexOf(target) == 0;
  }
}

function endsWith(subject, target, opt_endAt) {
  var tLen, i, subjectType = typeOf(subject), subjectIsArray = subjectType == 'Array';
  if (subjectIsArray || subjectType == 'String') {
    subject = subject.slice(0, opt_endAt);
    if (subjectIsArray) {
      for (
        target = toArray(target), tLen = target.length,
          subject = subject.slice(-tLen), i = tLen > subject.length ? -2 : tLen;
        i-- > 0 && subject[i] === target[i];
      );
      return i == -1;
    }
    target += '';
    return !target || subject.slice(-target.length) == target;
  }
}

startsWith(...) API Documentation

Determines whether or not a target is found at the very beginning of an array or a string.

Parameters

  1. subject {Array|string}:
    The array or string to be examined.
  2. target {*}:
    If subject is a string this will be converted to a string. This is the target which will be searched for at the very beginning of subject.
  3. opt_startAt {number}:
    Optional. Defaults to 0. This is the index at which target must be found in order for startsWith() to return true.

Returns

Returns true if target is found at opt_startAt (which defaults to 0). In all other cases this returns false.

endsWith(...) API Documentation

Determines whether or not a target is found at the very end of an array or a string.

Parameters

  1. subject {Array|string}:
    The array or string to be examined.
  2. target {*}:
    If subject is a string this will be converted to a string. This is the target which will be searched for at the very end of subject.
  3. opt_endAt {number}:
    Optional. Defaults to subject.length. If subject is a string, target must be found ending at this index. If subject is an array, target must be found at the index prior to this index.

Returns

Returns true if target is found ending subject. In all other cases this returns false.

Example

Using startsWith():

console.log(YourJS.startsWith('Hello', ''));  // -> true
console.log(YourJS.startsWith('Hello', 'Hel'));  // -> true
console.log(YourJS.startsWith('Hello', 'Help'));  // -> false
console.log(YourJS.startsWith('Hello', 'ello'));  // -> false
console.log(YourJS.startsWith('Hello', 'ello', 1));  // -> true
console.log(YourJS.startsWith('Hello', 'lo', -2));  // -> true

Using endsWith():

console.log(YourJS.endsWith('Hello', ''));  // -> true
console.log(YourJS.endsWith('Hello', 'llo'));  // -> true
console.log(YourJS.endsWith('Hello', 'plo'));  // -> false
console.log(YourJS.endsWith('Hello', 'Hell'));  // -> false
console.log(YourJS.endsWith('Hello', 'Hell', 4));  // -> true
console.log(YourJS.endsWith('Hello', 'He', -3));  // -> true