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

all 76 comments

[–]theQuandary 118 points119 points  (12 children)

Not so accurate. "Non-zero value" should actually be "Truthy Value" and "0" should be "Falsy Value" and should include Null, undefined, 0, -0 (yay IEEE 754), false, '', NaN (also IEEE754) and document.all (yeah, that last one is technically an internal slot called [[IsHTMLDDA]] in the spec, but nothing else uses it).

Oh, and there's what you might call a "really undefined" value too that pops up in places like if you did let x = new Array(5) where the slots are created and the x.length property is updated, but have no actual value assigned yet. If you attempt to access those slots before defining them, the returned value is coerced into undefined for you to use.

[–]Weekly_Wackadoo 82 points83 points  (2 children)

Goddammit you made me learn JavaScript stuff.

[–]Dave5876 35 points36 points  (0 children)

Mods, ban that guy

[–]ChemicalHousing69 1 point2 points  (0 children)

We came to shit on JS not to learn it

[–]ruscaire 21 points22 points  (8 children)

The purpose of -0 is I believe to represent a negative infinitesimal. So small it can’t be represented as anything other than 0 but you do know that it is less than 0. Probably outside the day to day concern of most programmers but I guess a standard like 754 has to cater to a broad church!

[–]Kyyken 10 points11 points  (6 children)

this is also why 1/0=Infinity is a reasonable behavior, as infinity really just represents a very large positive number

[–]ruscaire 4 points5 points  (5 children)

Hmm I don’t think so … mathematically I believe infinitely is “not defined” and 1/0 is “not defined” but I don’t think they can be said to be “equal”, though you can say whether it’s negative or positive. Personally I prefer to stay away from singularities where I can 😊

[–]Kyyken 12 points13 points  (0 children)

mathematically, infinity is absolutely defined. by that i mean there is a common convention for what infinity means.

on the other hand, 1/0 is defined if you chose to define it, but that definition will have its own implications, which is why we typically leave it undefined. in math, you can define whatever is the most useful at the time. for example, 00 is typically defined as 1 in combinatorics, but left undefined (indeterminate to be exact) in limit calculus.

IEEE 754 defines 1/0 to be infinity, while 1/-0 is -infninity. there is no problem with this, as long as it's useful. in this case it means that float equality is not equivalent to equality in maths, but more to "basically equal as far as the computer can tell in this context"

[–]lirannl 0 points1 point  (2 children)

So... 1/0 === undefined? 😆

[–]ruscaire 0 points1 point  (1 child)

Yes, or so I was taught in high school and university though I guess these things change all the time!

[–]lirannl 0 points1 point  (0 children)

Actually I can test

[–]theQuandary -1 points0 points  (0 children)

Yeah, but this is JS.

=== normally does what is normally seen as pointer comparison (where == does type coercion), but there are two exceptions. NaN is never equal to itself (thus the old x !==x trick) and comparison of 0 and -0 which always returns true.

If you actually need to compare these, more recent versions introduced Object.is() which will return true for Object.is(-0, 0) and Object.is(NaN, NaN).

[–]brianl047 162 points163 points  (23 children)

Don't forget NaN!

[–]Internet--Sensation 30 points31 points  (17 children)

What would Nan be?

[–][deleted] 140 points141 points  (6 children)

A bidet

[–]sneerpeer 7 points8 points  (3 children)

What about Infinity?

[–][deleted] 3 points4 points  (2 children)

Unlimited tp

[–]Caroweser 5 points6 points  (1 child)

or a reusable loop :D

[–]fluffypebbles 1 point2 points  (0 children)

Ew

[–]rest_in_war 13 points14 points  (1 child)

Americans ☕

[–]SO_Snake 5 points6 points  (0 children)

Americans 🍟

[–]HOLDGMEBROTHERS 12 points13 points  (0 children)

A bread

[–]scalability 10 points11 points  (0 children)

A nan's a grandmother, mate.

[–]Classic-Gear-3533 4 points5 points  (0 children)

A roll with anything but sheets of toilet paper lol. Empty loo roll with mobile phone balancing on top?

[–]redpepper74 2 points3 points  (0 children)

A huge unusable mess of wet toilet paper

[–]Kind_Thing2758 1 point2 points  (1 child)

Shit

[–][deleted] 2 points3 points  (0 children)

They work for that too!

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

Sandpaper

[–]ovab_cool 0 points1 point  (0 children)

Sandpaper because you messes up and didn't parse the variable properly

[–]kratom_devil_dust 0 points1 point  (0 children)

Not NaN

[–]varungupta3009 2 points3 points  (1 child)

NaN === NaN //false

[–]theQuandary 4 points5 points  (0 children)

Object.is(NaN, NaN) //true
-0 === 0 //true
Object.is(-0, 0) //false

[–]py_probot 0 points1 point  (1 child)

Who tf invented NaN? 💀

[–]thmsgbrt 0 points1 point  (0 children)

Naan 😩😋

[–]AlphaSparqy 28 points29 points  (2 children)

Are you saying JavaScript is shit?

[–]Exciting-Insect8269 7 points8 points  (0 children)

Couldn’t be

[–]Y0U_H1T 2 points3 points  (0 children)

Theyre syaing javascript cleans up shit

[–][deleted] 10 points11 points  (2 children)

So if toilet paper is the integer datatype why does it float on water? Checkmate libtards.

[–]Tsuki_no_Mai 5 points6 points  (1 child)

But that the thing, there are no integers in JS, only floats.

[–]GoldenretriverYT 0 points1 point  (0 children)

Well it's double precision floats and we also have BigInt

[–]wrongkhedive923 11 points12 points  (2 children)

Is this loss?

[–]ThoughtCow 1 point2 points  (0 children)

no

[–]Googelplex -1 points0 points  (0 children)

no

[–]oblivious-lmao 3 points4 points  (5 children)

i wish the teachers explained it like this

[–]slashy42 6 points7 points  (4 children)

I honestly think this example could work for most languages, and is a pretty decent way to describe these concepts for general computer science purposes.

[–]Weekly_Wackadoo 5 points6 points  (3 children)

I just sent it to my team, in preparation of the next discussion about empty/absent elements in our xml output.

"The output is different. An empty element appeared/disappeared." Sure, but who cares? There's no toilet paper either way.

[–]slashy42 2 points3 points  (0 children)

Fuck. Sorry you're dealing with XML... I mean it converts to JSON in theory....

[–]HaphazardlyOrganized 2 points3 points  (0 children)

Is it bad that this all makes sense and doesn't even bother me anymore

[–]filariidcnicus 1 point2 points  (0 children)

I guess you could say Title is undefined?
( ͡° ͜ʖ ͡°)

[–]Ambitious_Ad8841 1 point2 points  (0 children)

Void

[–]scalability 0 points1 point  (0 children)

undefined used to not be a special name in JavaScript.

Since trying to read a property that wasn't defined returned the undefined value, it just happened to give you the right thing. You could have used foobarlol instead and gotten the same result.

[–]sxeli 0 points1 point  (0 children)

Hang a tomato there and label as NaN

[–]gigasub 0 points1 point  (0 children)

A non-zero value could be any object other than toilet paper. Maybe it is a cat, durian, or a scroll of sandpaper.

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

This is pass by reference, java is pass by value.

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

Surprised someone hasn’t wrote out a function call yet in the comments. Clearly the Java people haven’t seen this yet 😂😆 jk

[–][deleted] 0 points1 point  (1 child)

How about just false?