Wtf-js Memes

Posts tagged with Wtf-js

Javascript Sorting

Javascript Sorting
JavaScript's Array.sort() is the embodiment of "task failed successfully." It gives you a sorting method that converts numbers to strings by default (because why wouldn't you?), mutates your original array like it owns the place, and produces the legendary [1, 10, 2, 21] result that's haunted every junior dev's dreams. Then, years of Stack Overflow questions later, JavaScript blessed us with Array.toSorted() - a non-mutating version that fixes exactly ONE of the three problems. You still need to pass a comparator function like (a, b) => a - b to sort numbers properly, and the docs still use that cursed [1, 10, 2, 21] example that looks totally fine until you realize it's lexicographically sorted. The gigachad energy here is JavaScript's unwavering commitment to backwards compatibility - even when the default behavior is objectively wrong for 90% of use cases. Beautiful chaos.

It's All There In The Specs, Bro

It's All There In The Specs, Bro
So you're telling me that accessing an array with a negative index in JavaScript not only works but actually adds a property to the array? And then when you check the array, it shows you this cursed -1: 4 sitting there like it belongs? The bell curve perfectly captures the JavaScript experience: beginners think it's ridiculous (correct), experts also think it's ridiculous (also correct), but the middle crowd has Stockholm syndrome and will defend it with their lives. "It makes sense bro, everything in JS is an object!" Yeah, and that's exactly the problem. JavaScript treats arrays like objects because they are objects, so test[-1] = 4 is just adding a property named "-1" to your array object. It's technically in the spec, which somehow makes it worse.