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

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.