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
> [,,,].length // -> 3 I don't think this is too weird if you think about it. JS allows trailing commas, so the last one is ignored. Effectively this is `[ undefined, undefined, undefined, ]`. A syntax error would have made sense here, but the length of three is a result of the usual syntax rules, not a particular strange quirk of JS.
JavaScript Is Weird
191–200 of 383 posts
Re: JavaScript Is Weird
#192I'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.
I think perhaps this "makes the most sense" from a language designer/implementor's perspective, but it also gives ample opportunity for WTF moments in actual use.
Re: JavaScript Is Weird
#193I 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.
GMail managed to be popular thanks to the XMLhttprequest() function being implemented in internet explorer. Microsoft had a monopoly on software, but it was still possible to do things through a web browser, enabling competitors and websites running on linux. Javascript got popular because it was here first, so developers used it and became able to work with it. When this happens, javascript had inertia which is impo…
And you could easily copy and learn from scripts before minification became the norm. And you just have to refresh some page after updating your sources to see the result.
Re: JavaScript Is Weird
#194Re: JavaScript Is Weird
#195> JavaScript is a great programming language, but ... Why are we so afraid to call trash "trash"? It's not attacking the creator, but just how can we make progress if things are not perceived as they are? https://wtfjs.com
The vast majority of these are implicit type coercion issues. Which is fairly easily banned with a linter. Once you've done that, I think JavaScript is probably the nicest of the big dynamic scripting languages (Perl, PHP, Python, Ruby) to work with. There could definitely be improvements, but trash seems to be taking things too far.
Re: JavaScript Is Weird
#196Earlier 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.
Re: JavaScript Is Weird
#197I 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.
WASM is not a good solution in a lot of situations. In short: - you have to package your complete runtime in the Wasm binary, this leads to a large binary and memory footprint - DOM manipulation requires calling Javascript code, and calling JS from WASM is costly
Languages that perform their own memory management (C, C++, Rust etc) will be extremely small. Just one big array of bytes/instructions.
Re: JavaScript Is Weird
#198Re: JavaScript Is Weird
#199Earlier quoted context omitted.
Yup, garbage in garbage out. I'm not a huge fan of JS, but this sort of criticism is absurd.
I rather have the language say "Error, this is garbage!" than silently output garbage.
Like most bad things, it's only around because a big company said so.
Like all bad things in JS, there was a push to remove it at ECMA, but Microsoft had reverse-engineered JS into JScript and refused to go along with the changes to fix the weirdness.