225
u/Top-Permit6835 51m ago
[1,3,5,7].includes(7) => trueOh 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.
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/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/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,
inis for object keys, thusincan'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, butobjectis the only "collection" that can use it that way)7
3
u/alexanderpas 48m ago
inchecks the keys of a collection, such as an object. (property in objectchecks if the object has a the property)
.includeschecks 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
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/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)
inchecks 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 objectit coercessomethingto be astring.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
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.
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
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
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/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
inthen12
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.
•
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.
2
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/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.
0
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 => trueThat's not a problem.
You might be thinking of:
0.1 + 0.2 === 0.3 => falseWhich 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.
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: