2

I was hoping someone could help me understand something about the '.includes' method.

My understanding was that this only worked on arrays? for e.g. myarray.includes('something').

But it also seems to work when you loop over the array and use it on an object for e.g:

    var people = [
        {
            name: 'Joe',
            age: 27
        },
        {
            name: 'Rob',
            age: 25
        },
        {
            name: 'Dave',
            age: 22
        }
    ];

    for(i=0; i<people.length; i++) {
        console.log(people[i].name.includes('Joe')) // True
    }

Can someone explain why this is?

Thanks,

Joe

1
  • 5
    String.prototype.includes — the "name" properties have strings for values. Commented Jul 25, 2019 at 14:01

1 Answer 1

4

Because name is of type string, which also has an includes method. You can read on that here.

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.