Live data from Hacker News

JavaScript Is Weird

jsisweird.com

171–180 of 383 posts

Re: JavaScript Is Weird

#171
post #168

Earlier quoted context omitted.

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

I agree. If native apis didn’t return nulls in some cases, and undefined was named “undef” at least, null could be ditched. But then again, it’s only because js has no bad habit of treating the same of non-existence and undefinedness^. If not json (which has no undefined), we could ditch null. But it’s there and with === it leads to either object.someField === null || object.someFeild === undefined madness, or to a p…

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.

Re: JavaScript Is Weird

#172
Ironically I got a bunch of these wrong because JavaScript is less weird than I thought it was. For example for some reason I thought ![] would be true, because JavaScript would do something weird, but it's not and JavaScript does exactly what you'd expect any sensible language to do.

I didn't enjoy the quiz, it just reminds me how depressing the base layer of the thing I'm most passionate about in life is. The expression [1,2,3]+[4,5,6] returning what it does is just a bummer, it just sucks. Calling it weird is giving it undue merit, as if it's cute somehow that it's so awful.

Re: JavaScript Is Weird

#173
I really think we should stop pushing things like that or the wat talk (https://www.destroyallsoftware.com/talks/wat). Sure it's funny and all, but it's not helping people understand. We're presenting Javascript as some kind of stupid black magic tool, which is not. You can very easily find the standard online, and go through the examples to understand. Sure it's unintuitive, I agree, but lots of things in programming are unintuitive, and when you can't change them you have to learn them.

Re: JavaScript Is Weird

#174
post #169

I'm actually pretty fond of JavaScript, but here's a question that has caught me out in real code: const x = [1, 10, 2], y = x.sort(); Now: what is the value of x? (Edit: and y?)

I've been using JavaScript for around 15 years, and I've never used a comma to separate expressions. And I'd still go to MDN to look up if sort() is in place or not :p

Re: JavaScript Is Weird

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

I think the situation is a bit different. This situation looks different in reality. The result may be (null-0)+"0", but the actual code will be foo()-bar()+baz(). And C will at least give you warning about types, even if NULL-0+"0" could give you an address. Plain JS without extra tooling would happily give you the unexpected result. Some other dynamic languages would at least throw an exception about incompatible t…

Because it's more to do with weak typing as opposed to dynamic typing. Many dynamic languages are strongly typed.

Re: JavaScript Is Weird

#176
post #142

Earlier quoted context omitted.

I rather have the language say "Error, this is garbage!" than silently output garbage.

Which is why we use Typescript

The fact that there has to be a different language on top of your language to make it same days all that needs saying really.

Re: JavaScript Is Weird

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

In C# you don't have such a problem and 0.2 + 0.1 is exactly 0.3. Proof:

https://dotnetfiddle.net/qTiq6U

Re: JavaScript Is Weird

#178
Probably unpopular opinion: Most of these are just the weirdness of the language caused by the (not-so-wise) design decision that those simple operators should never throw. In my opinion, these are not footguns and should not be used to attack JavaScript, because regular programmers are very unlikely to run into them (which is why when presented, they seem so obscure).

However, that is not to say JavaScript is without footguns. For example, the classic `[1, 2, 3, 10].sort()`.

Re: JavaScript Is Weird

#179
post #91
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…

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

This is a result of the implicit type conversion feature rather than a strict type system.

Re: JavaScript Is Weird

#180
post #79

Earlier quoted context omitted.

Why the hell can websites tell that I'm in the console? If I want to inspect some parts of a website (perhaps save an image), then I don't want that website to interfere.

While there are some ways to detect having devtools open[1], this website isn't actually using them. Instead, it just always writes these messages to the console. Before navigating to the answers, it clears the console so you don't see the messages that were logged before. [1]: https://stackoverflow.com/a/7809413/451847

This answer how facebook does (did?) it and why is also very interesting: https://stackoverflow.com/a/21693931
Post reply on HN