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 Is Weird
181–190 of 383 posts
Re: JavaScript Is Weird
#182Earlier 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!"; ?"
Re: JavaScript Is Weird
#1830.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
Re: JavaScript Is Weird
#184Earlier 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.
Re: JavaScript Is Weird
#185Probably 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
#186I'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
And that's the first gotcha: sort() is in place.
Re: JavaScript Is Weird
#187I'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?)
* Except Object.create(null), which throws.
Re: JavaScript Is Weird
#188Re: JavaScript Is Weird
#189I'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?)
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
#190Earlier 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.