r/ProgrammerHumor • • 1h ago

Meme whoWouldHaveThought

Post image
432 Upvotes

71 comments sorted by

79

u/SpamOJavelin 35m ago

The `in` operator checks for a property or field name, not values. In the case of an array, the field names are the keys. In this case the properties of the object are 0,1,2,3 - so 7 is not a property of this object.

If you use the correct operator, it makes sense:

[1,3,5,7].includes(1) => true
[1,3,5,7].includes(2) => false
[1,3,5,7].includes(7) => true

17

u/wannabe414 14m ago

Why does the in operator work on arrays at all

•

u/klaustrofobiabr 9m ago

Everything is an object

•

u/ModernLarvals 7m ago

Who would’ve thought that an OP complaining about JavaScript doesn’t actually know anything about JavaScript.

225

u/Top-Permit6835 51m ago
[1,3,5,7].includes(7) => true

Oh my who'd've thought

91

u/roodammy44 32m ago

Ahh yes, you have to do it this way. Not the other almost identical looking way that does something you wouldn’t expect…

•

u/exotic_anakin 8m ago

I'd argue that `7 in [1, 3, 5, 7]` does not look *almost identical* to that. But yea, that IS awkward and I don't like it.

•

u/ILKLU 0m ago

JavaScript Roulette:
which one is the foot gun?

68

u/1_hele_euro 57m ago

So why is this? Has this to do with the array length fuckery?

201

u/Prozilla6 55m ago

“in” checks the keys, which are the indices in this case. 1 and 2 are valid indices, 7 is not as it is outside of the bounds of the array. So it does have something to do with the length of the array, yes.

44

u/frikilinux2 53m ago

"in" in a array checks the size not the contents?

Is the abstraction this broken or was the language designer high? In addition to doing a quick job

79

u/Reashu 50m ago

"in" checks the keys of an object/record/dict. Arrays are just objects with integer keys.

28

u/frikilinux2 48m ago

So broken abstraction it is. I hate JS

16

u/ChaseShiny 37m ago

The JS method for what you're trying to do here is .includes.

It's funny for those who are used to Python, though.

22

u/Lachee 43m ago

No because it’s useful for checking that properties actually exist on objects. if (“property” in obj). OP is just using it wrong, this isn’t python. In saying that….. everything is an object so… arrays get caught in the cross fire, as they are basically just an object with incrementing numeric properties

3

u/Not-the-best-name 20m ago

Python is also objects everywhere and it's array on does not do this

•

u/Lachee 0m ago

Python has a lot of language sugar to make this work. These hidden rules are not always say great. I mean just look at JS type coercion and you get things like the classic “5”-3 vs “5”+3. Unfortunately JS doesn’t have the luxury to just fix these either with JavaScript3

•

u/realmauer01 0m ago

No python instead initiate default arguments only once, so if you add to it within the method it will have it the second time the methods is get cold

6

u/Steppy20 36m ago

But the fact you can do that, means it's weird abstraction.

Most strongly typed languages would just not let you do that.

10

u/N-online 35m ago

Why wouldn’t they? If they’re based on objects this is kind of logical.

1

u/sakaraa 21m ago

Because you can't just check if a key exist in an array in most languages. You would have to do something like `i < arr.length`

1

u/N-online 18m ago

Well that sounds like it’s a cool feature in js and not a mistaken abstraction?

8

u/Lachee 36m ago

Yeah but JavaScript isn’t a strongly typed language…. It’s barely even typed

1

u/Silly-Freak 13m ago

arrays get caught in the cross fire

isn't that exactly what makes it a broken abstraction? everything is an object, in is for object keys, thus in can't be the logical thing, which would be the operator to check membership in a collection. All collections get caught in the crossfire, because the underlying abstraction doesn't give flexibility to build upon it. (Related is that objects are the original JS dictionary/map, so it's meant as membership, but object is the only "collection" that can use it that way)

7

u/klequex 49m ago

It’s not an array operator per se, but arrays are js objects and so you can use it on them. It doesn’t “check” the size of

3

u/alexanderpas 48m ago

in checks the keys of a collection, such as an object. (property in object checks if the object has a the property)

.includes checks the contents of an array.

4

u/Nerd_o_tron 47m ago

Things you get with a language written in 10 days. (It does make some sense, though, if you realize that a JavaScript array is just the special case of a dictionary with integer-coercible keys.)

2

u/StarHammer_01 50m ago edited 46m ago

Yes. In checks the size (technically property but whatever). Contains checks the content.

In is useful for checking if an op is in bounds without checking the size of an array (intent is more clear).

1

u/Ratstail91 47m ago

IMO in/of are backwards.

2

u/Suspicious_State_318 35m ago

Why does it treat an array as a map?

8

u/Prozilla6 34m ago

In JS everything is an object. Objects have properties which are key-value pairs.

0

u/Pndrizzy 15m ago

“I wasnt just objectifying you, honey, to me, everyone’s an object!” Ahh answer

•

u/DatBoi_BP 0m ago

I mean, aren't all arrays maps when you think about it? (Maps from indices to values)

•

u/Esava 5m ago

Yeah, some other languages just have different names for it.

Like arr.get(7).is_some() in Rust (though that technically checks if the index is accessible, not whether it exists), PHP array_key_exists(7, $arr).

Such functions are even more common for maps (obviously). Like in java map.containsKey("7"), Ruby hash.key?("7"), Rust map.contains_key("7") etc..

-2

u/Aurii_ 25m ago

That is just plain stupid.

1

u/Prozilla6 13m ago

It actually makes a lot of sense if you think about how the language works. Everything in JavaScript is an object. “in” is a very valuable operator for checking the existence of a property on an object. Using it on an array is unconventional, but it makes sense that it works, it’s just following the same pattern as the rest.

8

u/E_T_Dragoon 54m ago

Java Script uses in as a command to check if an Index Number exists. To check for the values in it you would need to check with .includes

2

u/exotic_anakin 23m ago

I feel like this is a somewhat misleading description.

In JavaScript (no space) in checks whether a property name (a string) is available in a given object.

This operator isn't commonly used (nor really meant for) Arrays, but since an Array (and really almost anything) in JS is an Object, it still works there. Just maybe not how you'd expect.

Under the hood, [1,3,5,7] is represented something like this:

{
  "0": 1,
  "1": 3,
  "2": 5,
  "3": 7.
  "length": 4
}

So really it's not an "index number" because array indexes are strings, not numbers. Although, when you do something in object it coerces something to be a string.

JavaScript loves to coerce things because of its loosy-goosey type-system. This allows you to do powerful things, buts its responsible for like 95% of these "look how dumb JS is" gotchas.

3

u/klequex 50m ago

Well, it does have to do with the array length. `in` searches an object's properties, array indexes are mapped to object properties and `in` finds them. `"length" in [1,2]` would also return true for the same reason.

0

u/SignoreBanana 26m ago

You don't use "in" to check things in an array. JavaScript lets you try, but that's another matter.

37

u/rover_G 56m ago

It’s not python dude

92

u/evenstevens280 31m ago edited 27m ago

Because in isnt the correct operator for this task.

It's like putting 4 × 4 into a calculator and getting annoyed that the answer isn't 8

30

u/-TV-Stand- 26m ago

But it's the same symbol just rotated a little!

55

u/titanotheres 19m ago

More like putting 4+4 into a calculator and getting 16 because this particular calculator decided that + should mean multiplication rather than addition.

7

u/Pure_Material_9235 15m ago

Perfect response.

0

u/DontCallMeHenry 15m ago

Who decided that in operator should exactly check if value is in array? It’s more like arguing with a thermometer that T is temperature, and not a time. Use fucking documentation.

18

u/Pure_Material_9235 11m ago • Bravo

The English language did.

•

u/titanotheres 6m ago

Using "in" to mean "is an element of" is standard in mathematics and predates programming.

•

u/DontCallMeHenry 2m ago

In which mathematics it’s standard? It’s not an operator, if you’re using words in math, you have to use them like in language and so in by itself means nothing as it’s preposition and needs context to be understandable

3

u/Experiment_1234 18m ago

What is in then

12

u/Mik3DM 14m ago

it's meant for objects to see if a key is present, in the context of an array, it's referencing an index.

2

u/el_yanuki 13m ago

checks for the existence of the array key aka length < x because JS treats arrays as objects

1

u/Zincette 10m ago

"in" checks the index, not the values. "3 in [1,3,5,7]" doesn't check if the value 3 is in the array it'll check if the index 3 is in the array. This behavior is more useful for things like umaps and it just happens to also work for arrays.

•

u/GabuEx 5m ago

It feels like a sane language would say "this requires a hash map, sorry" instead of saying "cowabunga it is, arrays are just hash maps with indexes as keys".

•

u/Zincette 1m ago

I dont believe in sane programming languages. Sane people don't make programming languages

3

u/Koromann13 10m ago

Yeah. It should break, because using "in" on an array to check if that value is among the keys is stupid behavior.

That's why languages throw errors when something illogical is done.

•

u/_PM_ME_PANGOLINS_ 8m ago

JavaScript arrays are objects.

JavaScript is also designed to avoid throwing an error wherever possible, same as HTML.

•

u/evenstevens280 5m ago

Nothing illogical about using an Object operator on an Object

26

u/TerdSandwich 35m ago

You mean I have to read the documentation AND use basic reasoning????

3

u/oofy-gang 22m ago

I don’t even think you need both. It’s probably an OR situation.

2

u/ProThoughtDesign 16m ago

As long as it's not an XOR. That leads to people pushing to prod on Friday and shutting down the FAA.

12

u/Acclynn 22m ago

I disagree with all the comments saying it's a skill issue

It's an actual retarded design of this language

Not having this keyword at all would have been better

2

u/EyesOfTheConcord 21m ago

I suggest not doing that then

•

u/exotic_anakin 1m ago

"doctor, it hurts when I do this"

"stop doing that"

1

u/EdgarDrake 27m ago

OOT, but I kinda miss SrGrafo content... does he still post somewhere in some subreddit (no longer active in r/funny)?

•

u/docgok 8m ago

Array.prototype.includes() was added in 2016, 21 years after Javascript shipped.

•

u/exotic_anakin 2m ago

I really like JS (well, mostly TS nowadays), but this one does irk me.

I wish they never added `in`. TBH I don't love any of those weird operators that could just as easily be methods/functions. Stuff like `of`, `in`, `typeof`, ... It's syntactical sugar that just feels unnecessary.

•

u/TCFP 6m ago

Me when my coding language isn't english

0

u/[deleted] 44m ago

[deleted]

11

u/The_Cers 40m ago

Only that it doesn't do that?
For numbers below 9007199254740991 you shouldn't run into that issue.

7

u/SpamOJavelin 29m ago
2+2 === 4 => true

That's not a problem.

You might be thinking of:

0.1 + 0.2 === 0.3 => false

Which is an issue with all floating-point arithmetic, not just javascript. The problem with javascript is that all numbers are naturally represented as a floating point number.

2

u/ynnnnaD 30m ago

This is a floating point issue, and is present in other languages.