0
const obj1 = { name: "kim", age: 14 };
const obj2 = { name: "Steve", age: 20};
const arr  = [ obj1, obj2];

arr[0]["name"].includes("han"); // true

so this is the code that I am curious.
I think includes() method is for array, not for object.
but in this case arr is array but arr[0] is object. also arr[0]["name"] is property of object, then why is it possible to use includes method on this code?
I would be grateful if someone tell me what I know wrong

2
  • 2
    It won't output true. Instead, it should give false. BTW you are using includes on a string, not on object. if you would have used any substring from kim then you would have got true Commented Aug 23, 2021 at 4:11
  • 1
    arr[0]["name"] (or arr[0].name, same thing) is a string. See String.prototype.includes() Commented Aug 23, 2021 at 4:16

2 Answers 2

1

You can use includes on Arrays or on strings.

According to you code includes doesn't give true because It doesn't work on an object. Instead, you are using it on a string.

When you console

arr[0]["name"]   // kim

And what your code is checking is whether han is a substring of kim or not. As we know it's not so it will output false

const obj1 = { name: "kim", age: 14 };
const obj2 = { name: "Steve", age: 20 };
const arr = [obj1, obj2];

console.log(arr[0]["name"]);
console.log(arr[0]["name"].includes("m"));

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

Comments

0

includes() check if a sub string is present in the given string.

In your case :

arr[0]["name"].includes("han")

It should return false, as "kim" does not include "han".

1 Comment

You should elaborate to give as many details as possible.

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.