Live data from Hacker News

JavaScript Is Weird

jsisweird.com

51–60 of 383 posts

Re: JavaScript Is Weird

#51
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…

> That's not really a JS problem, that's a floating point problem

More accurately it's a binary problem. 0.1 and 0.3 have non-terminating representations in binary, so it's completely irrelevant whether you're using fixed or floating point.

Any number that can be represented as the sum of powers of 2 and 5 have a terminating decimal representation, whereas only numbers that can be represented as the sum of powers of 2 have a terminating representation in binary. The latter is clearly a subset of the former, so it seems obvious that we should be using decimal types by default in our programming languages.

Oh well.

Re: JavaScript Is Weird

#52
post #13

I use JS since almost a decade and only got half of them right. Specifically I failed at the last one because I got impatient, the octal one got me too (never use those in JS), the true++ and the ones that require you to know that arrays are converted to strings when used in arithmetic. I'll just externalize my embarrassment and say that JS is indeed weird! Another excuse is that I typically try to avoid implicit typ…

I have been using JS for almost 10 years, and have written quite a few big apps with it, both front- and back-end, but only a handful of times have I experienced this “weird” issues. Nobody writes code like that, I only know of these things because there have been many websites like this over the years.

Re: JavaScript Is Weird

#53

> 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.

> I think JavaScript is probably the nicest of the big dynamic scripting languages (Perl, PHP, Python, Ruby) to work with.

As an experienced Python programmer learning JavaScript, this isn’t true for me yet but I hope it becomes true. I think JS will be much more useful to me than Python.

> Which is fairly easily banned with a linter.

Any particular recommendations?

Re: JavaScript Is Weird

#54

> 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

Calling it "trash" is a very inflammatory way to describe one the most popular programming languages in the world. Language like that is against the HN guidelines, because it distracts from your argument to debate about whether JS is "trash" or not.

JavaScript has a bunch of quirks, but it's still great. In the StackOverflow survey, JS reliably ranks highly in the list of "most loved" languages. https://insights.stackoverflow.com/survey/2020#technology-mo...

Node.js is a thing because people liked JS so much that they wanted to use it on the server side. You may think they're all fools, but it's actually pretty nice. I like the latest version of Node better than working in Python (mostly because node_modules are better than virtualenvs, IMO.)

You can use TypeScript to address many of the quirks described in TFA, but you can also reliably avoid them just by never using == and always preferring ===, using String() before using + to concatenate, and using Number() before subtracting.

I basically never encounter situations like the ones depicted in TFA in real code, because I never try to add two arrays or subtract two strings or what have you.

Re: JavaScript Is Weird

#56

I 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.

"There are only two kinds of languages: the ones people complain about and the ones nobody uses." – Bjarne Stroustrup

JS may be weird, but the kind of weird that works well enough and is rather easy to pickup and maintain with reasonable rules in place. When it comes to JS my empirical experience has often been: most of the time, the issue is situated somewhere between the keyboard and the chair.

Re: JavaScript Is Weird

#57
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…

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

Re: JavaScript Is Weird

#58
These examples are amusing but irrelevant.

What I find sorely lacking is sane syntax for working with arrays. Eyeing Python with envy every time.

And some pattern matching would be nice too.

Re: JavaScript Is Weird

#59
post #30
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…

Exactly. Finding flaws in a language doesn't mean the language is bad, just that it has... flaws. The proof that JS is actually pretty good is that it has been used to build so many things. Like the old adage about economics, these criticisms are taking something that works in practice and trying to see if it works in theory.

Just because something is popular isn't reason for it to be good. Examples: fossil fuels, (over)fishing, rage-based engagement, hard drugs.

Re: JavaScript Is Weird

#60
post #53

Earlier quoted context omitted.

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.

> I think JavaScript is probably the nicest of the big dynamic scripting languages (Perl, PHP, Python, Ruby) to work with. As an experienced Python programmer learning JavaScript, this isn’t true for me yet but I hope it becomes true. I think JS will be much more useful to me than Python. > Which is fairly easily banned with a linter. Any particular recommendations?

Eslint is the default linter. Standard is a curated list of rules with good quality.

https://github.com/standard/eslint-config-standard

I would start with that and tweak what you don't like

Post reply on HN