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!"; ?"
JavaScript Is Weird
211–220 of 383 posts
Re: JavaScript Is Weird
#212Earlier quoted context omitted.
Both of these examples are well-known (and not unexpected) behaviours. I assume you already know why it behaves like that. If not, I can explain it. > [1, 2, 3] + [4, 5, 6] // -> "1,2,34,5,6" What would you expect instead? > [,,,].length // -> 3 Is there any use case where you would want to deal with sparse arrays?
[1,2,3] + [4,5,6] An addition operation over two numerical vectors of length 3. I would expect the result to match its inputs and provide a numerical vector of length 3. Thus: [5, 7, 9]
Re: JavaScript Is Weird
#213Earlier 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.
To "implement decimal numbers" in .net on x86 hardware simply means writing a custom decimal implementation in software.
JS had implicit integers since the beginning. It has had arrays of integers (what's necessary for fast decimal implementations) since BEFORE the release of WebGL a decade ago. It has also added BigInt this year. Just like .net, there's decimal libraries available if you know to use them.
https://github.com/MikeMcl/decimal.js/
The real takeaway is that modern processors should definitely add hardware support for decimal numbers.
Re: JavaScript Is Weird
#214Re: JavaScript Is Weird
#215I have limited knowledge in web development and its history, but why is javascript a first class language in web dev when all I heard is "javascript bad"? Web assembly seems like a much better choice in hindsight where you can choose different language to compile down to wasm.
Using WebAssembly currently means programming in C, C++, Rust, or maybe Go. Despite its flaws, many people prefer programming in JavaScript over any of those lower-level languages.
Re: JavaScript Is Weird
#216Re: JavaScript Is Weird
#2170.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…
Also things like +!![] is because while JS is dynamic it does have a very strict type system! ![] becomes a boolean +true becomes a number.
(+ (not (not '())))
The value NIL is not of the expected type NUMBER.
Similarly in Ruby: +!![]
undefined method `+@' for true:TrueClass (NoMethodError)
[Condition of type TYPE-ERROR]
Interestingly, Python does the same thing as JS in this case, even though it is typically quite strongly typed. Edit: not quite the same, as the empty array is converted to False in Python, just as it is in Ruby (in CL, nil/'() IS the canonical false value); but still, Python outputs 0, it doesn't complain like the other two.Re: JavaScript Is Weird
#218Judging by the title, it sounded like the biggest discovery of 2021... by someone waking after a long coma. It was discovered in 2012: https://www.destroyallsoftware.com/talks/wat I really enjoyed the questions... and even though I am working in TypeScript, I got only 9/25. Precisely because I avoid f---ed up parts of JS, except for trivia games ( https://dorey.github.io/JavaScript-Equality-Table/unified/ , http://ww…
There are a lot of people being born every year. If a 16-18 year old teenager is getting into web development, they were 4-6 when that talk was given in 2012. Sure, that talk still appears here and there, but it's past its prime and you'd have to be in the right place at the right time to run into it.
Crockford's book (JS: The Good Parts) was ubiquitous among JS devs who were coding between 2008 and 2015 (give or take). These days, younger devs have never heard of it. Due to the Seinfeld Effect, if they read it, it would seem trite and obvious not realizing how revolutionary it was at the time.
Re: JavaScript Is Weird
#2190.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…
I think the situation is a bit different. This situation looks different in reality. The result may be (null-0)+"0", but the actual code will be foo()-bar()+baz(). And C will at least give you warning about types, even if NULL-0+"0" could give you an address. Plain JS without extra tooling would happily give you the unexpected result. Some other dynamic languages would at least throw an exception about incompatible t…
Re: JavaScript Is Weird
#2200.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…
> 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. That is the whole premise of the site, though. They even say that these examples aren't common syntax or patterns before you start.