Checking if an array has an item with the vanilla JS Array.protype.includes() method

In yesterday’s article, I mentioned the Array.prototype.includes() method. Today, I wanted to look at how it works.
The Array.prototype.includes() method accepts the value to search for in an array as an argument, and returns a boolean, true if it’s in the array, and false if it’s not.
let wizards = [‘Merlin’, ‘Ursula’, ‘Gandalf’, ‘Radagast’]; // returns true let hasUrsula = wizards.includes(‘Ursula’); // returns false let hasMorgana = wizards.includes(‘Morgana’); If you want to start searching at a specific index, you can pass that in as an optional second argument.


This content originally appeared on Go Make Things and was authored by Go Make Things

In yesterday’s article, I mentioned the Array.prototype.includes() method. Today, I wanted to look at how it works.

The Array.prototype.includes() method accepts the value to search for in an array as an argument, and returns a boolean, true if it’s in the array, and false if it’s not.

let wizards = ['Merlin', 'Ursula', 'Gandalf', 'Radagast'];

// returns true
let hasUrsula = wizards.includes('Ursula');

// returns false
let hasMorgana = wizards.includes('Morgana');

If you want to start searching at a specific index, you can pass that in as an optional second argument.

// check if 'Radagast' is in the array anywhere from index 2 on
wizards.includes('Radagast', 2);

The Array.prototype.includes() method can only be used with shallow arrays containing primitive values. Arrays with nested arrays or objects won’t work, because they fail the internal equality that the method runs.

Level-up your team’s core JavaScript skills. I offer project-based courses and workshops designed to fit around your team's schedule. It's perfect for remote and distributed teams.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2023-02-09T14:30:00+00:00) Checking if an array has an item with the vanilla JS Array.protype.includes() method. Retrieved from https://www.scien.cx/2023/02/09/checking-if-an-array-has-an-item-with-the-vanilla-js-array-protype-includes-method/

MLA
" » Checking if an array has an item with the vanilla JS Array.protype.includes() method." Go Make Things | Sciencx - Thursday February 9, 2023, https://www.scien.cx/2023/02/09/checking-if-an-array-has-an-item-with-the-vanilla-js-array-protype-includes-method/
HARVARD
Go Make Things | Sciencx Thursday February 9, 2023 » Checking if an array has an item with the vanilla JS Array.protype.includes() method., viewed ,<https://www.scien.cx/2023/02/09/checking-if-an-array-has-an-item-with-the-vanilla-js-array-protype-includes-method/>
VANCOUVER
Go Make Things | Sciencx - » Checking if an array has an item with the vanilla JS Array.protype.includes() method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/09/checking-if-an-array-has-an-item-with-the-vanilla-js-array-protype-includes-method/
CHICAGO
" » Checking if an array has an item with the vanilla JS Array.protype.includes() method." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2023/02/09/checking-if-an-array-has-an-item-with-the-vanilla-js-array-protype-includes-method/
IEEE
" » Checking if an array has an item with the vanilla JS Array.protype.includes() method." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2023/02/09/checking-if-an-array-has-an-item-with-the-vanilla-js-array-protype-includes-method/. [Accessed: ]
rf:citation
» Checking if an array has an item with the vanilla JS Array.protype.includes() method | Go Make Things | Sciencx | https://www.scien.cx/2023/02/09/checking-if-an-array-has-an-item-with-the-vanilla-js-array-protype-includes-method/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.