Liam
Liam

Reputation: 11

Why is there a difference in the naming of `Array.prototype.includes` vs `Set.prototype.has`?

Both names seem to fit into the definition of "determine if a certain value is contained within this".

Standard built-in objects that have these method names

includes

has

It seems it is the internal implementation which determines the name, I am interested if there is a specific reason why these have different naming conventions.

const array = ["a", "b", "c"];
const string = "abc";
string.includes("a"); // true
array.includes("a"); // true

const set = new Set(["a", "b", "c"])
const map = new Map([["a", "a"], ["b", "b"], ["c", "c"]]);
set.has("a"); // true
map.has("a"); // true

Upvotes: 1

Views: 90

Answers (1)

frandroid
frandroid

Reputation: 1416

It's a semantic difference on the nature of each function and their related parent. Maps and Sets only have one instance of what you check for. That is why the categorical "has" verb is used. Arrays and strings might contain multiple instances of what you ask includes to check, but it simply returns true at the first instance found, unaware of whether there are more instances of the looked-for object.

Upvotes: 3

Related Questions