0

Hi I am New to JS i trying below code

a=[0,1,3]
if (2 in a){ console.log(a)}

in above code i am checking element is available are not. but in above case 2 is not in array but condition show true why?

i know we can check condition like this a.includes(2) why in above case showing true?

Thanks Advance.

2

1 Answer 1

5

Becase the in operator checks whether a property is defined in an object.

The array indexes are the "properties" of the array objects, thus, the 2 property is the index 2, which contains the value 3.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.