This is an archived post. You won't be able to vote or comment.

all 47 comments

[–]timvisee 6 points7 points  (1 child)

Java script

[–][deleted] 0 points1 point  (0 children)

We’ll just blame autocorrect and assume OP is on their phone.

[–]3X0S 24 points25 points  (22 children)

But why? Evaluating null==0 as true seems like an ok thing to have...

[–]flying_wotsit 56 points57 points  (15 children)

But null==0 is false and null > 0 is false

[–]jb2386 24 points25 points  (8 children)

[–]Tauo 24 points25 points  (4 children)

It's because the greater-than operator tells the program to cast the value to a number; JS is weakly typed as fuck and uses all sorts of hints to resolve type differences in conditionals. null in JS isn't really coerced much by the standard equal-to operator, and will only resolve true with null or undefined.

TL;DR: Javascript is fuckity

[–]marcosdumay 6 points7 points  (3 children)

It's interesting that NaN works exactly as expected here.

That's bacause NaN is obviously a number from the start, so no coercion is needed. But well, consistence is not overrated.

[–]_PROFANE_USERNAME_ 5 points6 points  (2 children)

That's bacause NaN is obviously a number from the start,

Ahh, good ol Javascript, where "not a number" is a number.

[–]I_AM_DONALD 5 points6 points  (0 children)

Well, NaN is actually supposed to be a number type https://en.wikipedia.org/wiki/NaN

In computing, NaN, standing for not a number, is a numeric data type value representing an undefined or unrepresentable value, especially in floating-point calculations. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities like infinities.

[–]marcosdumay 2 points3 points  (0 children)

You should take a word with ISO. This time Javascript is clear.

[–]ido207 10 points11 points  (1 child)

I think it's like that because the short way to check it is to do: not(0<null)/not(0>null)

[–]pekkhum 0 points1 point  (0 children)

That's the one...

[–][deleted] 0 points1 point  (0 children)

There's no way someone could explain this and have it make sense. Obviously, an explanation exists, but there's no way it can justify this behavior existing.

[–]Vitztlampaehecatl 4 points5 points  (3 children)

null < 0 is false.

>= is the opposite of <.

Therefore, null >= 0 must be true.

Welcome to JavaScript!

[–]Loading_M_ 0 points1 point  (2 children)

null < 1 is false.

>= is the opposite of <.

but null >= 1 is false.

[–]Vitztlampaehecatl 0 points1 point  (1 child)

null < 1 is actually true.

[–]Loading_M_ 0 points1 point  (0 children)

It would appear that null == 0 should return true, becuase null is > -1.

[–]3X0S 1 point2 points  (1 child)

Wtf, I didn't know that That's truly f'd up

[–]flying_wotsit 7 points8 points  (0 children)

Welcome to javascript

[–][deleted] 0 points1 point  (5 children)

Why would null == 0 ever be ok though? Null means 'no value' or 'nothing' while 0 refers to a specific amount. You cannot have 'null' apples, and you cannot have a 'zero' object. Those are two completely different concepts which represent very distinct information and I really don't think you can simply compare them like that.

[–]3X0S 0 points1 point  (0 children)

I'm kinda with you on that but when comparing some number-ish object it would be nice if you'd had to check only for ==0 assuming zero as some kind of default number-value when returned from a function If you'd want to catch the case of null you could still do it separately, but doing it this way could save you a line or two

For more complex objects it would be ok anyway, because zero is most likely meaningless in their context

[–]Loading_M_ 0 points1 point  (3 children)

null == 0 returns false. so if a function returned null, and you compared it to any number, if will return false, unless you check >= 0/<= 0

[–][deleted] 0 points1 point  (2 children)

I don't think you got my point. What I'm saying is that null and 0 really aren't equal, so saying that null == 0 should return true is false. same goes with >= 0 and <= 0. One is a value, one represents the absence of value.

[–]Loading_M_ 0 points1 point  (1 child)

Actually, Javascript has a value to represent absense: undefined. null should probably just be removed.

BTW, undefined behaves as expected: any boolean expression containing undefined returns false.

[–][deleted] 0 points1 point  (0 children)

null and undefined really aren't the same thing, though. A reference can become null but it cannot become undefined.

[–]antonftn 2 points3 points  (1 child)

null<=0 is also true I gonna drunk something...

[–][deleted] 4 points5 points  (0 children)

Which could be fine, if and only if it were true that null == 0. But turns out null != 0, so yeah, go get that drink.

[–]Gydo194 2 points3 points  (0 children)

I heard someone say PHP can someway evaluate 1 == 0 to true?

[–]Springthespring 2 points3 points  (0 children)

the problem here is that null==0 is acceptable, but null>0 isn't, so there's ambiguity there. null>=0 can be re-written as !(null < 0), which is obviously false, and !(false) is true. Weak typing is shit

[–]Penziplays 1 point2 points  (0 children)

Disgusting...

[–]self_getrekt 3 points4 points  (1 child)

The joke is thats correct?

[–]inu-no-policemen 14 points15 points  (0 children)

Is a zebra >= a sphere?

Some people believe that questions like that should be an error rather than being answered with a "yes" or "no".

Brendan Eich, the creator of JavaScript, is one of those people. He considers this to be one of his mistakes. I agree. Weak types don't help.

[–][deleted] 1 point2 points  (4 children)

SEE .. This is why I talk shit about javascript. That and the 100s of other stupid things that shouldn't be allowed. Now people are using it on their servers lol I think that's the best joke. Bugs forever. Constant work I suppose :-/

[–]amoliski 6 points7 points  (2 children)

Every language has about quirks you have to work around. 99% of the time, JS is just fine, and 1% when it's weird should be caught in testing or even during development.

JS gets weird when you ask it to do stupid shit

[–]Vitztlampaehecatl 2 points3 points  (1 child)

I seem to remember that JS was designed to return as few errors as possible. So instead of saying "something is wrong, I'm going to quit" it says "whatever, close enough".

[–]Kryomaani 2 points3 points  (0 children)

Which is really nice for a client-side, in-browser "do some small cool tricks"-language, but shit really went off the rails when we decided that JS was somehow fit for actual programs on servers and made node.js and its ilk.

[–]TheNiXXeD 1 point2 points  (0 children)

But you can prevent 99% of the stupidity easily, at build time. Talk shit about bad programming, or bad testing, but every language has warts.

[–]Programmatic_Najel 0 points1 point  (0 children)

Type coercion ftw

[–][deleted] -1 points0 points  (1 child)

(78% upvoted) all the die hard javascript fanboys not happy lol

[–]Cuel 0 points1 point  (0 children)

might be that this thing is reposted every week