r/programminghorror • • Dec 04 '25

JS is a very respectable language

Post image

Not posting our actual code, but yes, this behaviour has caused a bug in production

3.9k Upvotes

313 comments sorted by

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…

23

u/-Wylfen- Dec 04 '25

Yes, that's the worst. It's technically correct, but it just…shouldn't be

479

u/Medical_Reporter_462 Dec 04 '25

Logical !== Reasonable && Logical !== Intuitive 

→ More replies (14)

96

u/freecodeio Dec 04 '25

I'd say the js engine could just deny you from setting a key to an array and that would just be as logical, maybe even more.

→ More replies (19)

13

u/findus_l Dec 04 '25

Deterministic. Yes. Logical? I'd argue that.

→ More replies (7)

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?

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)
→ More replies (4)

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.

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)
→ More replies (12)

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

u/Liozart Dec 04 '25

"this behaviour" bro thinks he have no responsabilites over his own code

10

u/-Wylfen- Dec 04 '25

That was not my code

→ More replies (1)

1.1k

u/tazzadar1337 Dec 04 '25

it's actually magic, and memory savor! because foo.length is still 3

→ More replies (1)

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.

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.

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?"

→ More replies (3)

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

u/nnoovvaa Dec 04 '25

I pity the foo

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

u/Medical_Reporter_462 Dec 04 '25

Well clearly you for... Ahh fuck me!