Live data from Hacker News

JavaScript Is Weird

jsisweird.com

261–270 of 383 posts

Re: JavaScript Is Weird

#261
post #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…

While true, there will be rational numbers you can't represent as floating-point numbers no matter which base you choose. And the moment you start calculating with inexactly represented numbers, there is a risk that the errors might multiply and the result of your computation will be incredibly wrong. This is the much bigger "problem" of floats, not the fact that 0.3 is not "technically" 0.3, but off by some minuscule number.

Re: JavaScript Is Weird

#262
post #51

Earlier quoted context omitted.

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

In pure mathematics, you can get perfect precision with non-terminating fractions. For example, 0.(6) + 0.(3) = 1 is true. The decimal (or binary) representation is just "syntax sugar" for the actual fraction - in this case, 2/3 + 1/3 = 1; or, if you prefer, 10/11 + 1/11 = 1, or 0.(10) + 0.(01) = 1. Note: I'm using a notation for infinitely repeating decimals that I learned in school - 0.(6) means 0.6666666...; 0.(01…

Yes, it's a theorem that every rational number can be represented by a decimal number that is either terminating or repeating.

Re: JavaScript Is Weird

#263
post #244

I’ve taught programming to people who struggled because they were misusing JS and the answer they were getting back coincidentally worked for some inputs but not others. Imagine trying to explain to them that this was their fault? Even trying to find the words to explain what happened made me feel bad that their first experience programming was tarnished by a language that will gladly allow you to do nonsense.

Can it be that JS is not good starting language then? Maybe strongly typed compiled languages would be a better fit? One can argue that if you want to program it might be too much information for starters - but IMO basic types are such a fundamental concept that it would be better to teach that concept early on. It also clears up a lot of newbie confusion as you cannot assign string to an int by mistake with strongly…

The problem with strongly typed languages is that it makes it infuriating whenever you have to work in a language without the same type guarantees. JavaScript made me want to throw my computers off the top of a skyscraper until I discovered typescript, and typescript just barely makes things slightly tolerable.

Re: JavaScript Is Weird

#264
post #250
post #186

Earlier quoted context omitted.

The comma is not material here: the answer is the same if we write `const x = ...; const y = ...`. And that's the first gotcha: sort() is in place.

Pretty sure I’ve actually tripped on sort() myself before by assuming it was a new array. It does make sense for performance though, creating a new array would be a bad default choice for performance, for the same reasons that forEach is generally faster than map all else being equal.

True. I like Ruby's solution with exclamation marks, such as `sort!`, to make this kind of behaviour obvious.

Re: JavaScript Is Weird

#265

Earlier quoted context omitted.

null and undefined is one of the things Javascript actually got right imo. Undefined is what lets Javascript turn what other dynamic language would throw as a runtime error into a value. "I do not have a definition for the thing you want" and "the thing you want is known to be unknown" are two totally different concepts that languages with only null to lean on must collapse into a single concept.

The problem is undefined is used in places null makes much more sense (like a result of the find function). I follow the rule to never use null and convert to/from undefined if needed by some library.

Leave it to javascript to pull defeat from the jaws of victory.

Re: JavaScript Is Weird

#266
Eh. I'm not a JavaScript developer, just winging it I got 9/25. Seems like there are 3 broad categories of (my) confusion. 1. floating point. Floating point is tricky, and when I use it seriously I have to look up a lot of special cases. I often forget I can get Nan, and it'll infect everything it touches. This is common in just about any language.

2. One or two of those , questions got me. I don't _hate_ it, but there's a little frustration there. Mostly I just need to know the syntax. If I was writing javascript for money, I imagine it would bite me once or twice, then I'd just memorize the rule.

3 (the biggie) implicit conversion. Oof. seems like a lot of types can be converted to other types, and it's not obvious to me what the precedence hierarchy is. With C I have to look up the edge cases around signed/unsigned, but usually it's just use the next bigger one int -> long. Java does the .toString for everything which has bitten me once or twice. I really don't have a sense of javascript implicit conversion. Some of those seem really subtle.

Re: JavaScript Is Weird

#267

Earlier quoted context omitted.

Someone had linked on here a website that showed the 0.3 thing in practically almost every programming language and their output. I wish I could remember the domain / url cause it is interesting to compare languages defaults. I know most languages have ways to handle it correctly.

I know most languages have ways to handle it correctly. Including JS - http://mikemcl.github.io/decimal.js/

There's a proposal to add a decimal type to the spec, too.

https://github.com/tc39/proposal-decimal

Seems like it's a long ways away from being available, though.

Re: JavaScript Is Weird

#268
post #91

Earlier quoted context omitted.

Also things like +!![] is because while JS is dynamic it does have a very strict type system! ![] becomes a boolean +true becomes a number.

JS has a very weak type system, full of implicit conversions. In dynamic languages with strong type systems, like Common Lisp, such operations typically result in errors: (+ (not (not '()))) The value NIL is not of the expected type NUMBER. Similarly in Ruby: +!![] undefined method `+@' for true:TrueClass (NoMethodError) [Condition of type TYPE-ERROR] Interestingly, Python does the same thing as JS in this case, even…

> Interestingly, Python does the same thing as JS in this case, even though it is typically quite strongly typed. [...] > Python outputs 0, it doesn't complain like the other two.

yep, this is one of those Python weird bits. in Python, booleans are ints, True is 1 and 0 is False. and i don't mean it in a JS-ish way like "they can be converted to...". no, True is the integer value 1. in fact, the type bool is a subtype of int. if you think of types as sets of values, and subtypes as subsets of their parent set, that `book

  >>> +(not not [])
  0
  >>> (not not [])
  False
  >>> 0 == False
  True
  >>> 1 == True
  True
  >>> True + True
  2
  >>> True - False + True * 0.5
  1.5
  >>> isinstance(False, bool)
  True
  >>> isinstance(False, int)
  True
  >>> bool 
so, if you accept that the operation `not []` makes sense to be defined as True (because `bool([])` is False), and that it makes sense for False to be the integer 0, then `+(not not [])` being 0 is just a logical consequence of that :)

for the record, i do think it's weird for Python to define bools as ints, and to make all values define a boolean semantics via __bool__().

Re: JavaScript Is Weird

#269

Earlier quoted context omitted.

I keep having this argument with my boss but he refuses to let me write machine code.

If the good lord wanted us to code in assembly language, he'd have made transistors operate on mnemonics, not electric currents.

If the good lord had wanted us to interact with transistors based on mnemonics, not electrical currents, he'd have implemented our brains in mnemonics, not electrical currents.

Re: JavaScript Is Weird

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

The root of the problem is the original intent of Javascript. Javascript was intended to be a small layer of dynamism added to web pages that were mostly defined via HTML which were presented to a human for interpretation. When your user agent is a human trying to look up an address for a restaurant, they can look at a garbled piece of crap where the JS crashed and still maybe find what they were looking for in the rendered text. Limp along the best you can is a great failure strategy for Javascript's original use-case. Only now that we've turned Javascript into a general purpose programming language is this a failure.

Even Javascript's weak typing makes sense in this case. Why automatically convert everything? Becausethe expectation was that inputs to your Javascript would be HTML attributes which are all strings. Automating type conversions from strings made sense. But once you move to larger scale programing, Javascript's weak typing is awful

Post reply on HN