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
3.9k
Upvotes
r/programminghorror • u/-Wylfen- • Dec 04 '25
Not posting our actual code, but yes, this behaviour has caused a bug in production
920
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…
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?
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.
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.
14
u/Liozart Dec 04 '25
"this behaviour" bro thinks he have no responsabilites over his own code
1.1k
u/tazzadar1337 Dec 04 '25
it's actually magic, and memory savor! because
foo.lengthis still 3207
u/edave64 Dec 04 '25
The whole point of
atis that it behaves differently for negative indices51
u/HebrewHamm3r Dec 04 '25
Is this an array? A map? Why not both?
61
u/stoppskylt Dec 04 '25
LGTM
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.
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.
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
refers to value at given index But - it inherits Object prototype so it have also properties like
So when you access
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!
4
u/0xlostincode Dec 06 '25
This is horror but at least is explainable horror unlike some other JS quirks.
1
u/codejunker Dec 04 '25
If this caused a bug in production the solution is as simple as not using negative numbers as indexes for arrays. Why would you ever do that in the first place? This is one of those "hurp durp I got JS to give me behavior that is unexpected!" type things that all you have to do to avoid is not be stupid.
1
u/codeguru42 Dec 05 '25
May I ask why are you using negative indexes?
1
u/IcyManufacturer8195 Dec 05 '25
Well, well, well another poor understanding of js object behaviour
1
u/NoInfluence5747 Dec 05 '25
Another post where a skill issue is passed as a js problem. If you're indexing with negative numbers on array/list on any language without checking exact behavior it's ur skill issue
1
u/No_Industry_7186 Dec 05 '25
Skill issue
1
u/SCP-iota Dec 08 '25
Bell curve moment. It's very clear what's happening here if you understand that arrays are just objects:
foo[-2]is field access notation - you're setting it like a dictionary key. The ability to access array items with field access is a convenience feature.foo.at(-2)is using the actual array indexing method, so it behaves as expected for negative indices.26
u/VibrantGypsyDildo Dec 04 '25
Excuse me, what? It is worse than C.
3
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Dec 04 '25
So why does JSON bother having arrays and objects if arrays can have keys and values too?
8
u/lumponmygroin Dec 04 '25
Why the fuck would you do this anyway?
It's a programming language, why would you even expect it to handle weird stuff like that?
1
u/KCGD_r Dec 04 '25
writes weird program
Language: gives weird response
"Guys the language sucks"
0
u/Bob_thezealot Dec 04 '25
Js bad haha post number infinity yet the dude is not posting actual code
6
u/thisiswarry Dec 04 '25
[-2] is ["-2"]
1
u/Ronin-s_Spirit Dec 05 '25
Yet another confused newcomer post, big whoop.
4
u/codey_coder Dec 04 '25
God’s chosen programming language. Do not use negative indices in brackets, Pythonist blasphemer.
4
u/SenpaiRemling Dec 04 '25
r/firstweekcoderhumour
4
u/nephelokokkygia Dec 04 '25
This is not horror because arrays don't exist in the traditional sense in JavaScript — they're just a type of object. For that reason, they inherit the behavior of other objects, like arbitrary property assignment. If you accessed the "-2" property before assignment, it would rightfully return
undefinedbecause arrays don't assign values to negative integers, only positive ones in range."But that's bad design!" No, it's just a design. One that you should understand fully if you want to write code in a competent way. There's no fundamental objective reason that negative integers should access properties in reverse order, it's just a feature that could exist. And in fact, it does exist by the
atmethod for those who wish to use it.4
u/marquoth_ Dec 04 '25
"We wrote bad code that broke prod. Clearly this is the language's fault."
2
u/totallynormalasshole Dec 05 '25
No offense, but someone wrote code that tries to set index -2 of an array and JavaScript is the problem?
5
u/EliselD Dec 04 '25 edited Dec 04 '25
Having worked with JS/TS for 6 years this makes a lot of sense to me. That being said I can completely understand how it seems completely random to someone unfamiliar with the unorthodox inner workings of JS. Eventually you develop a 6th sense for this kind of gotchas, but I agree that is extremely un-intuitive and it should change.
To shed some light on what is going on to those unfamiliar with JS the most important thing to know is that arrays in JS are actually objects masquerading as arrays.
Under the hood the array looks kinda like this:
The reason why
foo[-2]andfoo.at(-2)return different values is because those two ways work differently:foo[x]thexis used to access thevalueof the property where thekey == x. In the example abovefoo[-2]means: return me the value of the property having-2askey(which is4). It is equivalent to just writingfoo.xif it makes more sense.foo.at(x)works a bit more to what people expect wherexis an integer value that indicates the index of the element to be returned from the array. A negative integer means you start counting from the end. But why doesfoo.at(-2)then return2in the example above instead of3you might wonder. Well that is indexes can only be positive integers and because-2is a negative integer it's not a valid index. You can even see in the screenshot above that(3)prefix before the array which indicates the length of the array (a.k.a number of properties where the key is a positive integer) , not the total number of object properties (which is why the propertieslengthand-2aren't counted).2
u/[deleted] Dec 06 '25
This is the reason why I still enjoy coding in C (industrial embedded code) - it's painful, but predictable.
3
u/AtomizedSparcles Dec 04 '25
I learned perl in the 90s and I learned ”there are many ways to do a thing, but some of the ways are just wrong”
This pattern seens to keep repeating. Take a look at PHP type coercion tables.
Compare the simple mathematical range of C and scripted languages. Try it with fibonacci series in different languages, see how the values start to diverge as internal implementation of numbers in some version of the floating point start to deviate.
In search of a perfect programming language…
6
u/Probetag Dec 04 '25
If u cant use programming language correctly or care to use the wiki. Negative Indices only work with Array.at()
4
u/BDHarrington7 Dec 04 '25
So many of these JS detractors have never programmed in assembly and it shows.
3
u/FACastello Dec 04 '25
Absolutely disgusting
2
u/nosrednehnai Dec 04 '25 edited Dec 04 '25
> Does something stupid that would cause normal languages to crash, completely ignoring JavaScript's use case
> "JavaScript bad"
Man, I am hating the internet more and more these days. Reddit used to be so much better in the early 2010s before these people.
1
u/cenekp Dec 04 '25
r/countablepixels
1
u/Feathercrown Dec 04 '25
Oh no
1
u/point5_ Dec 04 '25
I don't know js but every time I see something like this, it makes me think that it tries it's hardest to never throw an exception even when it should because wdym you can have an array with negative indexes?
1
u/kobaasama Dec 04 '25
First time?
1
u/goos_ Dec 04 '25
Lmao WTAF
1
u/zikjegaming Dec 04 '25
Also a great quiz about the insane Dates in js: https://jsdate.wtf/
1
u/ECrispy Dec 04 '25
It was written in 10 days and is the most widely used programming language. Not bad
1
u/Shadedlaugh Dec 04 '25
That's in lua too
1
u/SSBeastMode Dec 04 '25
Indexing starts at ZERO in REAL programming languages
1
u/Nixinova Dec 04 '25
Not the languages fault you don't know the difference between built-in index access vs a prototype function that has purposefully different functionality...
1
u/BenjaminGeiger Dec 04 '25
wat
1
u/euodeioenem Dec 04 '25
okay i dont get why foo[-2] == 4 tho
1
u/F2BEAR Dec 04 '25
This is why I don't trust people doing vanilla js and always enforce the usage of typescript and eslint. Everything is an object in js and I had seen too many times people doing things like this to solve things that should have been solved doing other things, but some times it's easier to just mess with the engine instead of putting the effort and then they introduce random bugs that may be (and most of times are) hard to find.
1
u/xehpuk Dec 05 '25
RTFM
1
u/a4andrei Dec 05 '25
I've been using it for too long that I knew what this will do 😅
1
u/cantor8 Dec 05 '25
If you really want an array, you have to write something like this :
let foo = new Float32Array([1, 2, 3])
Then the behavior will be very different
1
u/PeekyBlenders Dec 05 '25
Someone should make the effort to rewrite the code in these memes or soon they'll all be lost to history!
1
u/obi_wan_stromboli Dec 05 '25
JS really just lets you do anything
1
u/No_Indication_1238 Dec 05 '25
Stuff you'd know if you ever looked at JS's internals, btw. This is very basic.
1
u/lmg1337 Dec 05 '25
JS never seizes to amaze me with BS like this
1
u/evasive_btch Dec 05 '25
This looks like completely expected behaviour.
1
u/ClementineBear Dec 05 '25
As a rule of thumb, I almost never use
foo[x]=to set an array index value to anything in any language. It’s simply one of the easiest methods to create all kinds of subtle bugs. There are a myriad of safer tools for manipulating arrays in safer ways, from a functional/immutable approach where you map the values on to a new array with whatever modifications need to be made to traditional slice, splice, push, pop, and other methods available in mutable language.1
u/allinvaincoder Dec 06 '25
Why is foo[-2] 4?
1
u/WhatIsSixTimesSeven Dec 06 '25
Wat?
1
u/Jesus_Chicken Dec 07 '25
If you dont like this, you got options!
1
u/11matt556 Dec 07 '25
Yeah JS is technically the first programming language I learned in highschool because at that point I was interested in web development.
But I got tired of this kind of nonsense from JS and now try to avoid it as much as possible for work. I much prefer Python or C#, but for me even C was more intuitive to learn than JS.
1
u/codeptualize Dec 09 '25
I get it, but also who cares. Just don't do weird stuff and it's fine.. Doing foo[-2] should just be red flags all over the place. In my many years writing JS with people of various experience levels I have not seen this once.
Every language has quirks and pitfalls, this is one of JS, just like you should use === instead of ==, and default arguments in Python are only evaluated and set once.
Every language has them, if you are not aware you will get burned at some point and you'll learn it the hard way. You can then complain the language is no good, or just learn and move on.
1
u/PickltRick Dec 09 '25
This is cow shit language
1
u/Pball1001 Jan 02 '26
So is it an array that contains an object? Or a dictionary/parameter:value within foo, separate from the array/list?
1
u/deceze Dec 04 '25
For a language that is basically locked forever into all of its sins, they really should've spent a little more time getting the basics right and sane, instead of implementing it in a week or whatever. Oh well, hindsight is 20/20…
1
u/forloopy Dec 04 '25
I mean seems like something that could be caught with minimal tests. Blows my mind people just wing shit and think it works because it “looks right”. I really doubt I’d ever assume how structures work in a language I clearly am not well versed in
1
u/DeductiveFallacy Dec 04 '25
Just a reminder, JS was hacked together by 1 dev over the course of 2 weeks by someone that was JUST hired, and we've been dealing with that ever since. It was only created to extend the capabilities of HTML for Netscape, rather than a rigorous process for creating a new programming language.
1
u/jizzstealingthiefman Dec 04 '25
the design is very human
1
u/Zeilar Dec 04 '25
I mean you should never allow a dynamic index to be a negative int. And using the
atmethod is preferable, there's a reason it was released.This post is just skill issue.