Live data from Hacker News

JavaScript Is Weird

jsisweird.com

121–130 of 383 posts

Re: JavaScript Is Weird

#122

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] // -> "1,2,34,5,6" > What would you expect instead? If I let my first instinct speak: [1,2,3,4,5,6] And then if I think a little more, then maybe: [5,7,9] //with obvious caveats In no way do I expect what GP actually provided.

I understand where you are coming from. But the addition operator simply does not have any special handling of arrays. The specification [1] clearly defines what it should be used for: "The addition operator either performs string concatenation or numeric addition." As JavaScript is weakly typed, it is the programmer's responsibility to use proper value types with these operators. That limitation (or advantage?) is also well-known and applies to all weakly-typed languages.

[1] https://tc39.es/ecma262/multipage/ecmascript-language-expres...

Re: JavaScript Is Weird

#124

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.

In general, a scripting language for a software product (a “browser” being one of them) may be easily substituted in most cases. Because this integration always has a clear API border than can be retargeted to another scripting language in a straightforward way. E.g. vim text editor has vimscript as its main scripting environment, but may be compiled (in addition) with perl, python, lua, tcl, ruby interfaces.

But guys who control web standards resisted for decades to suggestions for other languages, though it was fine to have flash/activex for the same period of time, until Apple killed it for good. Even JS “2.0” (a theoretical better but incompatible version of itself) had no chance, because reasons.

Javascript is okayish generally, but its unusual parts come from the times when it was used as a glue between textual inputs and some~ data structures.

Stroustrups’ phrase is just a stockholm syndrome (in his case self-induced).

Edit: webassembly support is not really required to transpile anything to a browser, see asm.js

Re: JavaScript Is Weird

#125
post #89
post #72

Earlier quoted context omitted.

https://english.stackexchange.com/a/30012/7762

Yes, the quotation from dictonary.com exactly supports what I am saying. It's a piece of spoken dialect not standard English. It's as if I was after using hiberno-English constructions in a HN comment. Will you go on out of that.

[deleted]

Re: JavaScript Is Weird

#126
post #89
post #72

Earlier quoted context omitted.

https://english.stackexchange.com/a/30012/7762

Yes, the quotation from dictonary.com exactly supports what I am saying. It's a piece of spoken dialect not standard English. It's as if I was after using hiberno-English constructions in a HN comment. Will you go on out of that.

There is no such thing as standard English. The link says only that the usage is informal, and HN comments are not formal writing.

Re: JavaScript Is Weird

#127
I have plenty of complaints about Javascript, but most of these aren't it. I haven't done any serious javascript in something like 6 years, and I still got most of this right without much effort.

#4, #13, #14, #21 and #24 are floating point problems and are nothing to do with JavaScript.

A bunch of them are just "learn the language" issues:

#2 I only got wrong because I never noticed JS allows trailing commas. Trailing commas are pretty damn common in most modern languages though. The syntax for empty initialisation of arrays is weird (and I'd argue is a mistake), but the behaviour is exactly what you'd expect.

#5 is the comma operator as also seen in C and C++.

#8 is something nobody should get wrong. Three nots in front of a true is false. Of course it is.

#10 is just an octal literal. They're becoming increasingly rare in modern languages, and were never quite as common as hex literals, but they're also not terribly obscure.

#15 is one I'm slightly ashamed I got wrong. It makes no sense to use increment operators on a bare value. This is common to just about all languages that support increment operators (and it's those, if any, that don't agree are in the wrong)

#22 is a slightly surprising counterpoint, but it also makes sense. I don't know of a language that has a NaN literal, and JavaScript is no exception. So that NaN is actually a global that is bound to the floating point value NaN. Because it is a global, it can be incremented (but incrementing NaN is still NaN).

Everything else is a type coercion issue.

#18 and #23 are coercions I really disagree with. Coercing things to a number shouldn't produce NaN. That truly is crazy. Throwing is the only reasonable behaviour here. Ruby is even worse than JS here ("true".to_i produces 0), Python gets it right.

Other than that, the coercions are all fairly make sense, and are consistent with what other languages do. Now, implicit type coercion (especially under the guise of abstract equality) is a terrible idea because it's easy to do it by accident and get nonsensical results, but the coercions themselves are, by and large, pretty sane.

Re: JavaScript Is Weird

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

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?

Re: JavaScript Is Weird

#130
post #100
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…

The same for “== considered harmful”. I scanned the entire comparison table and the only unobvious or error-prone cases are those you never really do in programming. https://stackoverflow.com/a/23465314 For me it’s only rows [[]], [0], [1], i.e. array-unfolding related, but all others are regular weak-typed comparisons like in perl and other dynamic semantics. Edit: just realized “if (array)” is okay, nevermind.

undefined and null sometimes make problems. IMHO it's good that undefined == null but some people don't realize.
Post reply on HN