Live data from Hacker News

JavaScript Is Weird

jsisweird.com

151–160 of 383 posts

Re: JavaScript Is Weird

#151
post #131

Earlier 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]

The issue there is that arrays aren't first class types in JS, they're just objects with numeric property names.

So if applying an operator to arrays spread the operation across all the elements that way, it would imply that the same should happen generally for all object properties, with whatever weird implications that would entail.

    a.foo = 1
    b.foo = 'there'
    a + b // { foo: '1there' } ?

Re: JavaScript Is Weird

#152
post #134

Earlier quoted context omitted.

In Go: https://play.golang.org/p/G8jOKtIj4AY

Can anyone explain this to me?

!!!true is equal to false in JS, and in Go (and in any other language I can think of.)

In that Go example above, the author is reassigning true and false to be their opposites.

Re: JavaScript Is Weird

#153
post #59
post #30

Earlier quoted context omitted.

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.

Excepting fossil fuels, none of those things are popular.

Re: JavaScript Is Weird

#154
post #49

Fortunately nowadays including any of those in your code would award you the review equivalent of bashing your head in. With the advent of linters most of these patterns started being rightfully considered bad form by default. Currently even innocent stuff like +new Date() (outputs a timestamp) is already something that I've rarely seen make it through review. JS is goofy, but ES2015 managed to avoid making the same…

I am eternally surprised at how long it took node to support ESM. CommonJS modules served their purpose, but they are about to go the way of JS module systems. This is where the appeal of deno comes in. It's a runtime that tries very, very hard to give you the same API surface as that of the browser. I just hope deno catches on faster.

Re: JavaScript Is Weird

#155

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

Trash

“Admitting like this has a bunch of quirks, but it’s still great.” (not arguing for it though)

Criticism is a driving force behind change to the better. Calling something “great” without pointing out great sides I find… pointless and harmful.

To name a few great points, distinct from other languages:

JS has a pretty shameless object model, where you (usually) have ordered keys, and every key is a property with an {enumerable, writable, configurable, get, set} descriptor. It is much more usable and practical than in almost all other languages. (They do that for “efficiency”, but that’s non-sequitur)

JS has very useful destructuring syntax, which many languages lack of and that leads to assignment bloating and boredom. Also, jit optimizes out temporary objects, so foo({x, y}) / function foo({x, y}) works almost at the same speed as foo(x, y).

JS has a nice Function type, which allows easier metaprogramming and substitution of ‘this’ object. Functions are objects, so you can function foo() {}; foo.x = 1; console.log(foo.name, foo.x).

JS bare objects may have methods and properties: t={_x:1, get_x(){return this._x}, …}

JS doesn’t treat syntactic lists in a last-value-only way, so you can expect foo(…a, …b, c) to work, and to work as intended. E.g. Lua cannot do that, and python (afair) requires you to collect items into a single array (not sure, correct me if I’m wrong).

But what people usually mean by “great” is “it generally works”. I know it is an american thing, but some aspects of js, including those you meet everyday, are really just trashy. It would be nice to not have them at all.

Re: JavaScript Is Weird

#156
post #142

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

Which is why we use Typescript

Re: JavaScript Is Weird

#157
post #134

Earlier quoted context omitted.

Yeah, a lot of these have nothing to do with JS. I have no idea what the site is trying to accomplish. I mean: !!!true In what language does that (or its equivalent) not evaluate to false?

In Go: https://play.golang.org/p/G8jOKtIj4AY

Great point, I'll edit my comment to say "(or its equivalent)", instead of "(that precise sequence of characters, no matter what you've redefined true and false to be)". Not sure why I originally wrote it that way.

Re: JavaScript Is Weird

#158

I think a lot of these errors can be fixed with using strict types, aka Typescript. I've had the same issues with PHP in the past too, which makes me believe that whosoever created these languages had ease of use as a higher priority than strictness, i.e. it's okay to assign bool, int, string to the same variable.. just get the job done.. okay. On the other end of the spectrum is a programming language like Pascal (o…

strict types and Typescript are not synonyms. Perhaps you meant to write “for example” instead of “aka.” And furthermore there tends to be some disagreement about the meaning of the words strong/weak, static/dynamic, strict/lax when talking about type systems. Indeed Typescript is quite a weak typesystem in that type checking does not very fully guarantee useful correctness properties about your program because it al…

I love the HN comment section — one of the best I’ve seen on the web. But nitpicking this use of a.k.a. doesn’t add anything to the conversation. We all know what they meant, since virtually no one thinks that TypeScript is the only strictly typed language in existence.

Re: JavaScript Is Weird

#159
post #117

Nobody programs like that, so it's not an issue in real-world applications.

I would agree if wasn't for the fact that JS is a dynamically typed language. You can (and sometimes will) run into instances of this without noticing, because while you won't explicitly write true++ you might do something like x = someFunction() x++ not realising that someFunction() might return a boolean under some circumstances. Anyone who worked with a sufficiently large JS codebase ran into one of these cases an…

This is true, one must always make sure to handle all possible return types, but that's true for all dynamic languages.

Re: JavaScript Is Weird

#160
post #69
post #63

Earlier quoted context omitted.

> that weird of a syntax ought to be "that weird a syntax": it's 'so [adjective] a [noun]' not 'so [adjective] of a [noun]'. It's 'so much of a syntax' but that's because 'much' is a determiner not an adjective, which really makes a much difference. Or if you want to be plain-spoken you can ofc just say 'such a weird syntax'.

English is as weird as JavaScript.

no, it's fine - you just need the right education https://publicdomainreview.org/collection/english-as-she-is-...
Post reply on HN