(in) Keyword problem in Js

I am not able to understand why my variable x is not true. Tried multiple ways to check if test is in arr

First of all, don’t post pictures of code, rather cut and paste it in. It’s much easier to help you.

Next, the in operator… I haven’t used it much, but I think that checks for keys, not values. You’re using an array to the keys are numbers.

Traditionally to check if an array has a specific element, you’d use the prototype method findIndex and see if the index is -1 or not.

1 Like

While the code seems to look good - the question is where you got the idea that this is how “in” works. Because as kevin mentioned, that’s not the way to check it.

It can be troublesome, but you have to keep some references around for the methods you use. Just because something looks like it makes sense, doesn’t mean it actually works. Especially such shorthands can give a false sense of “that looks right”, while it really doesn’t. Regardin the “in”-keyword, your approach would actually work in Python, so this can be further deceiving.

Yeah, that is a good way to say it. Humans can intuit what you mean if it “makes sense” - but computers are really dumb in that respect.

It can be troublesome, but you have to keep some references around for the methods you use.

For me, with JS, that is the MDN site. If that fails, a google search. I am a professional developer and don’t have a single book here. Not because I have it all memorized (far from it) but because it is faster to find it online.

Regardin the “in”-keyword, your approach would actually work in Python, so this can be further deceiving.

Yeah, I was wondering if I’d seen that before, other than JS…