Design & Download
Your JS Library
in Seconds!!!

Snippet: includes()

Written by Christopher West (cwest) on November 07, 2015.
Determines if a target is found in the Array or string.
function includes(obj, target, opt_fromIndex) {
  var index = indexOf(obj, target, opt_fromIndex);
  return index !== -1 && index != undefined;
}

In ECMAScript 6 Array.prototype.includes() and String.prototype.includes() have been added. The functions are to be used to determine if a target is found in the Array or string. As is evident, not all environments will have these functions defined. Fortunately, since this is a snippet, now you can use a similar function in YourJS. The main difference is that YourJS.includes() will also search object property values.

Parameters

  1. obj {Array|Object|string}:
    The array, object or string to be searched.
  2. target {*}:
    The value to be searched for within obj.
  3. opt_fromIndex {number}:
    Optional. Defaults to 0. When searching an array or a string, this is indicates the starting index in the search. When searching an array, a negative value will start that many positions from the end.

Return

If target is found in obj, true will be returned. Otherwise false is returned.