Let's dive into the explanation provided by Mozilla Developer:
The includes()
method is versatile as it does not specifically require an Array object, making it applicable to various other types of objects (such as array-like objects).
An example demonstrating the use of the includes()
method on the function's arguments object is showcased below.
(function() {
console.log(Array.prototype.includes.call(arguments, 'a')) // true
console.log(Array.prototype.includes.call(arguments, 'd')) // false
})('a','b','c')
While array-like objects share similarities with Arrays (like having a length
property), they lack functionalities such as map
and slice
. Another instance of an array-like object is HTMLCollection
, obtainable through
document.getElementsByTagName('div')
in your browser's console.
Comparing the prototypes of
document.getElementsByTagName('div').__proto__
and
[].__proto
will reveal differences in their getters and setters, although both provide the
length
property.