Live data from Hacker News

JavaScript Is Weird

jsisweird.com

131–140 of 383 posts

Re: JavaScript Is Weird

#131

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

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

#132

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 allows for missing type annotations.

Re: JavaScript Is Weird

#133
post #97

I stopped at the first example (true + false). If you write code like this, you have bigger problems than using JavaScript. For most of the examples, it's like saying "Here's what happens when you plug a fan to a faucet". There's no point really. JavaScript is weird just like any other language can be weird when you use it in weird ways. And yes, it's certainly one of the weirdest. If you want to talk about things we…

> I stopped at the first example (true + false).

So you never did something like this:

  x = performCalculation(getParam1())
  y = performCalculation(getParam2())
  sum = x + y
only to discover that "performCalculation()" sometimes returns a boolean instead of a number? JS is a dynamically typed language after all and functions like

  function f(x) {
    if (x >= 0) {
      return Math.sqrt(x)
    } else {
      return false
    }
  }
are perfectly valid. If you use a 3rd party library that uses return values like this, you might run into such case without realising it.

Sure, you won't explicitly write "true + false", but "f(x) + g(x)" is not uncommon and might indeed evaluate to "true + false".

Re: JavaScript Is Weird

#134
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?

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

Re: JavaScript Is Weird

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

true++ and x++ are syntactically different though. The latter is a post-increment of an identifier, and the former is a post-increment of a boolean literal. Different rules may apply to these cases.

Re: JavaScript Is Weird

#136

Earlier quoted context omitted.

> [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 a…

It seems that by "expected", you mean "expected, by anyone who read the spec" which I don't think is a fair use of that word. Obviously, most JS developers have not and will not read the spec.

I am very happy with JS and TS and I think the coercion rules are easily worked around with linter rules and policies, but they are definitely weird and I think the language would be better if it simply threw exceptions instead. But then, such an issue shoud not be surprising for a language that was designed in 10 days.

Re: JavaScript Is Weird

#137

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

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?

> Is there any use case where you would want to deal with sparse arrays?

Not really. Now explain why [,,,].map((e,i) => i) is [,,,] instead of [1,2,3] please ;)

Re: JavaScript Is Weird

#138
post #43

> JavaScript is a great programming language I have no idea how you could come up with this sentence after this brilliant display of what an insanely stupid language JavaScript is.

To be fair, just because a language allows you to do stupid things, doesn't mean that it's bad. C, C++, Perl (oh, Perl!) allow you to do weird things, too, so do lots and lots of other languages. A certain knowledge of this weirdness is very helpful, though, in avoiding it.

In fact Perl 5 has less weird coercion rules than JavaScript.

Re: JavaScript Is Weird

#139

Especially weird was that the quiz stuck at question 2 and kept resizing and shifting the position of the question text on selecting any answer until the quiz ended. CSS in the console is a nice touch though.

It doesnt work for me in Firefox at all.

Re: JavaScript Is Weird

#140

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.

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 impossible to stop. Javascript allows one to deploy anything on any platform with a web browser, without copying files.

Webassembly is great, but it's not easy to build WASM files, the toolchain software used (compilers linkers etc) are not mature (only rust can build to WASM natively), and it requires that all language compile to wasm, so compiler developer need to implement a WASM compile "target", which takes time and is not always possible depending on language (python comes to mind, because of its large library, global interpreter lock, etc).

Also, WASM doesn't have access to the DOM or webGL, meaning that you still need to make JS calls to interact with a webpage.

Post reply on HN