Live data from Hacker News

JavaScript Is Weird

jsisweird.com

181–190 of 383 posts

Re: JavaScript Is Weird

#181
post #178

Probably unpopular opinion: Most of these are just the weirdness of the language caused by the (not-so-wise) design decision that those simple operators should never throw. In my opinion, these are not footguns and should not be used to attack JavaScript, because regular programmers are very unlikely to run into them (which is why when presented, they seem so obscure). However, that is not to say JavaScript is withou…

JavaScript sorting numbers alphabetically is a fact I simply cannot get over. I know I should be pragmatic as an engineer but this goes against the core of my being. Awful.

Re: JavaScript Is Weird

#182
post #103

Earlier quoted context omitted.

Some of them are stretched examples, others comes from other languages/constraints (floating point, octal, ...), but some other are legitimately weird and error prone: [1, 2, 3] + [4, 5, 6] // -> "1,2,34,5,6" [,,,].length // -> 3

I could also make a php is weird page and say: "WAT $a .= "World!"; ?"

Yes. You could.

Re: JavaScript Is Weird

#183
post #19

0.2 + 0.1 === 0.3 That's not really a JS problem, that's a floating point problem. Plenty of languages will have the same issue. +!![] "" - - "" (null - 0) + "0" Calling these things weird is fair enough but I can't help thinking this is code you'd never actually write outside of the context of a "Look how weird JS is!" post. It's like picking examples from the Annual Obfuscated C Contest to show how hard it is to un…

In C# you don't have such a problem and 0.2 + 0.1 is exactly 0.3. Proof: https://dotnetfiddle.net/qTiq6U

In every language where you can use a decimal the result will match exactly. That's... not what's discussed here and it's not a default in c# either.

Re: JavaScript Is Weird

#184

Earlier quoted context omitted.

In C# you don't have such a problem and 0.2 + 0.1 is exactly 0.3. Proof: https://dotnetfiddle.net/qTiq6U

In every language where you can use a decimal the result will match exactly. That's... not what's discussed here and it's not a default in c# either.

Unlike other languages, in JS you don't have decimals... So you stuck writing a garbage code by multiplying all floating point numbers by a factor to avoid rounding errors.

Re: JavaScript Is Weird

#185
post #178

Probably unpopular opinion: Most of these are just the weirdness of the language caused by the (not-so-wise) design decision that those simple operators should never throw. In my opinion, these are not footguns and should not be used to attack JavaScript, because regular programmers are very unlikely to run into them (which is why when presented, they seem so obscure). However, that is not to say JavaScript is withou…

JavaScript sorting numbers alphabetically is a fact I simply cannot get over. I know I should be pragmatic as an engineer but this goes against the core of my being. Awful.

Since it’s (imo) default-wrong in English, I’ve chosen to think of it as a nice way to point devs to something that’s locale-aware like Intl.Collator instead. If it “just worked” in English, most of us would never think to use the correct utility instead.

Re: JavaScript Is Weird

#186
post #174
post #169

I'm actually pretty fond of JavaScript, but here's a question that has caught me out in real code: const x = [1, 10, 2], y = x.sort(); Now: what is the value of x? (Edit: and y?)

I've been using JavaScript for around 15 years, and I've never used a comma to separate expressions. And I'd still go to MDN to look up if sort() is in place or not :p

The comma is not material here: the answer is the same if we write `const x = ...; const y = ...`.

And that's the first gotcha: sort() is in place.

Re: JavaScript Is Weird

#187
post #169

I'm actually pretty fond of JavaScript, but here's a question that has caught me out in real code: const x = [1, 10, 2], y = x.sort(); Now: what is the value of x? (Edit: and y?)

In JavaScript every possible value has a .toString() representation* so it makes the most sense that the untyped/mixed array .sort() does lexicographic sorting. If you need numeric sort, use a TypedArray or provide an explicit arrow function.

* Except Object.create(null), which throws.

Re: JavaScript Is Weird

#188

Earlier quoted context omitted.

Which is why we use Typescript

The fact that there has to be a different language on top of your language to make it same days all that needs saying really.

I keep having this argument with my boss but he refuses to let me write machine code.

Re: JavaScript Is Weird

#189
post #169

I'm actually pretty fond of JavaScript, but here's a question that has caught me out in real code: const x = [1, 10, 2], y = x.sort(); Now: what is the value of x? (Edit: and y?)

Is the y relevant? Sort is in place and without a comparator function, it's going to convert the numbers to strings, so the array will remain the same.

If the question is about const, const with Arrays/Objects are by reference rather than value, nothing is stopping them from being modified later in the code.

Re: JavaScript Is Weird

#190

Earlier quoted context omitted.

In every language where you can use a decimal the result will match exactly. That's... not what's discussed here and it's not a default in c# either.

Unlike other languages, in JS you don't have decimals... So you stuck writing a garbage code by multiplying all floating point numbers by a factor to avoid rounding errors.

I don't think that's fair: C# has decimals but everyone uses floating point numbers by default so C# developers still need to know that 0.1+0.2 != 0.3
Post reply on HN