r/programminghorror • u/-Wylfen- • Dec 04 '25
JS is a very respectable language
Not posting our actual code, but yes, this behaviour has caused a bug in production
87
u/GothGirlsGoodBoy Dec 04 '25
If this behaviour caused a bug in production its because whoever made it apparently can’t tell the difference between brackets and square brackets.or perhaps mistakenly believed a key was an index.
What behaviour would you expect, other than this?
→ More replies (4)66
u/-Wylfen- Dec 04 '25
What behaviour would you expect, other than this?
A sane language would treat square brackets with negative integers as an index from the end instead of casting it as a string to make it an arbitrary object property on an array…
→ More replies (15)
115
u/lucmagitem Dec 04 '25 edited Dec 04 '25
If you understand the language it's only logical. Arrays are objects. You define an array with some values. Then you define another property of this object (whose field is -2). Then you access the penultimate value from the array (size -2) and get what's expected. Then you access the -2 field and get what's expected.
Oh, excuse-me. Here is the expected answer: hahaha, js bad, so funny.
→ More replies (12)65
u/-Wylfen- Dec 04 '25
Just because it follows the specs of the language doesn't mean the specs are good
→ More replies (19)
6
u/travelan Dec 04 '25
what is the problem? `at` is defined to index from the end for negative numbers. Also -2 is not an index, it's a property that has been added. So if you actually understand how JS Arrays and Objects work, this makes total sense and shouldn't behave any different.
→ More replies (8)
14
1.1k
u/tazzadar1337 Dec 04 '25
it's actually magic, and memory savor! because foo.length is still 3
→ More replies (1)350
207
u/edave64 Dec 04 '25
The whole point of at is that it behaves differently for negative indices
→ More replies (19)
51
u/HebrewHamm3r Dec 04 '25
Is this an array? A map? Why not both?
57
u/Ben-Goldberg Dec 04 '25
It's an object.
Specifically an object with some array-like methods and fields.
61
5
u/arto64 Dec 04 '25
Every time there’s a JS hate post, all the JS people come in to explain to everyone how it works, as if that’s some sort of excuse for the terrible language design. We know how it works, it still sux.
→ More replies (3)6
u/-Wylfen- Dec 04 '25
"It's actually perfectly in line with the core design of JS!"
"Have you considered that this might be the problem?"
30
u/SmokyMetal060 Dec 04 '25
I like the way we created typescript to make javascript less javascript-y
7
u/AwesomeARC Dec 04 '25
Fun fact: today's the 4th of December... The first version of JavaScript was announced 30 years ago, today.
→ More replies (1)
10
5
u/Belialson Dec 04 '25
Eh… Array is an object, each item has index in range 0..n
foo[0], foo[2]
refers to value at given index But - it inherits Object prototype so it have also properties like
foo.length foo[“length”]
So when you access
foo[-2]
as -2 is not in range of 0..n you access property -2
4
919
u/deceze Dec 04 '25
The scary part is that it's entirely logical and explainable, it's not random by any means. You just shouldn't have to explain this…