-2

From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

Object.keys()
Returns an array containing the names of all of the given object's own enumerable string properties.

This says that 'includes' works on strings: https://www.w3schools.com/jsref/jsref_includes.asp

Then why is the following statement valid in the code shown in this SO thread?
https://stackoverflow.com/a/66758849/462608

const colorValues = Object.keys(this.colorValues);
const invalidColors = colors.filter(c => !colorValues.includes(c));
3

1 Answer 1

3

It exists on both prototypes: String.prototype.includes and Array.prototype.includes.

String.prototype.includes checks if the string contains the passed substring.

Array.prototype.includes checks if the array contains a particular value (with the SameValueZero algorithm).

Somewhat confusingly, they use the exact same name, but the logic employed is similar.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.