Javascript Inludes( ) Method





What is the Includes( ) Method?



The Includes( ) Method allows you to determine if there is a specific element in an Array or String and it will return true or false as appropriate.

--> Especially when there are a lot of elements or information, this method is helpful to make sure certain elements are in the correct numberic position!



Example code:

				
Ex. 1:
// var a = [1,2,3,4,5];
a.includes(3); // true
a.includes(6); // false



Ex.2:
//Array includes() method
//check to see if something is inside an array

let colors = [ 'red', 'blue', 'yellow'];

['red', 'blue', 'yellow'] .includes(blue); // true
['red', 'blue', 'yellow'] .includes(purple); // false
['red', 'blue', 'yellow'] .includes(blue, pink); // false
['red', 'blue', 'yellow'] .includes(red, blue); // true
['red', 'blue', 'black'] .includes(black); // true

	
	


Syntax:



arr.includes(searchElement)

arr.includes(searchElement, fromIndex)


searchElement --> is the element to search for

fromIndex --> is the position in the array where to begin the search for the searchElement. The default position system starts at 0

The return value is called a Boolean -- a.k.a. true or false





--> What is an Array or String?