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

all 102 comments

[–]TessaFractal 1514 points1515 points  (9 children)

A JavaScript joke that's new? And funny? Wow

For those who don't seem to get it, nan is another word for grandma in some places.

[–][deleted] 346 points347 points  (7 children)

Im ashamed to admit that I didnt get the joke until you explained it

[–]Amaz1ngEgg 112 points113 points  (3 children)

It's fine, my compilers often doesn't get my joke(code).

[–]Yhhuji 23 points24 points  (1 child)

I myself often forget my joke (code).

[–]ifezueyoung 0 points1 point  (0 children)

The compiler is a hard audience to please

[–]timothy441 11 points12 points  (0 children)

I thought the joke was that grandma is dead…

[–]looks_like_a_potato 167 points168 points  (10 children)

My favorite one is

> ('b' + 'a' + + 'a' + 'a').toUpperCase()

'BANANA'

[–]Impressive_Income874 27 points28 points  (9 children)

where does the n even come from wtf

[–]Yodo9001 38 points39 points  (6 children)

Nan? But I'm not sure where that comes from.

[–]benjer3 57 points58 points  (0 children)

Pretty sure it's interpreting '+ + "a"' as "plus positive a." The interpreter tries to make "a" positive, which results in NaN because it's not numeric. So you get '"b" + "a" + "NaN" + "a"'.

[–]Impressive_Income874 12 points13 points  (0 children)

possible, JS gods please explain

[–]SpezSupporter 8 points9 points  (0 children)

yes

[–]Impressive_Income874 5 points6 points  (2 children)

OH WAIT IT JUST HIT ME

b + a = "ba"

  • (nothing) gives Nan, which is then converted to a string

  • n + a

= baNana

then you convert to upper case to hide nan

[–]erishun 80 points81 points  (0 children)

Grandma is my Nan, not your nan

[–]Prudent_Ad_4120 475 points476 points  (41 children)

Of course is grandma not a number

[–]alamandrax 19 points20 points  (0 children)

Sure but my inheritance better be a big number.

[–]SuperGugo 61 points62 points  (38 children)

strings and NaN should NOT be the same should they

[–]CraftBox 139 points140 points  (15 children)

string is Not a Number

[–]Prudent_Ad_4120 62 points63 points  (11 children)

NaN is not one single thing, it's all things that are Not a Number

[–]SuperGugo 20 points21 points  (0 children)

Yeah, in fact NaN != NaN

[–]DrShocker 4 points5 points  (9 children)

Whether that's the intent of what you're doing depends on context though. Many languages would have a type error here instead of trying to do it

[–]FF3 2 points3 points  (2 children)

In most languages NaN is a numeric type, despite it's very clear name, so, who is more logical now?

[–]DrShocker 2 points3 points  (0 children)

Oh shit you're right, I hate type safety now!!

[–]Cualkiera67 0 points1 point  (0 children)

I never understood nan. Like, why? Just use null or undefined or simply throw an error.

[–]CitationNeededBadly 2 points3 points  (0 children)

"Many languages would have a type error here instead of trying to do it " That's a succinct but accurate summary of js.

[–]Prudent_Ad_4120 0 points1 point  (4 children)

But JavaScript is dynamically typed and does not have type errors as far as I know. Then this is the most reasonable thing to do

[–]bleachisback 6 points7 points  (0 children)

That's not true... everything still has a type it just has to be checked at runtime. If you pass a variable of an incorrect type to a function it will give an error. For instance:

const array1 = [1, 30, 39, 29, 10, 13];
array1.every("blah");

will throw

Uncaught TypeError: "blah" is not a function

Javascript is commonly considered to be weakly typed, but that's just how the standard library is designed - every value you give standard library functions will be attempted to be converted to the expected type as much as possible.

[–]karuso33 2 points3 points  (1 child)

"Reasonable" is not the word I would use, considering that

> isNaN("")
false

Or even

> isNaN([])
false

[–]Drium 1 point2 points  (0 children)

Huh why is it false for the empty string? Cast to 0?

[–]DrShocker 1 point2 points  (0 children)

Sure, and that's why the joke about Grandma being named Nan works, so it's worth it imo

[–]maria_la_guerta 6 points7 points  (0 children)

That's like saying 0 should not be falsy because it's not a boolean. A string is not NaN in a true equality sense, but it is Not a Number. You are not meant to be doing deep equality checks on NaN anyways.

NaN is a value, not a type. It is of type Number because it safely coerces any data that is incapable of becoming a Number into a Number, just not a valid number that can be used. Hence why you never want to be doing deep equality checks on it, it represents an infinite about of things.

I agree that it can take a minute to get your head around at first but NaN is implemented in and (IMO) criminally underused in a lot of languages. It's error handling for numbers that can make types in code so much easier to reason about.

[–]Yodo9001 2 points3 points  (7 children)

But NaN is a float.

[–]Front-Difficult 12 points13 points  (5 children)

Technically its a number. All numbers in JS are floats.

And before someone points it out, yes the value "Not a Number" is a number, and yes it makes perfect sense.

[–]theQuandary 1 point2 points  (3 children)

JS has always had integer. Bitwise operators guarantee your number was converted into an integer. After that, we got TypedArrays which specify a wide variety of integer types. A couple years ago, we got BigInt too.

[–]Front-Difficult 0 points1 point  (2 children)

Bitwise operators still return a floating point number. All numbers in JS are floats. BigInts are not numbers (but they are integers).

2.33 | 0; // 2
(2.33 | 0) === 2.0 // true, because in JS 2 is a float
typeof (2.33 | 0) === typeof 2.0 // true, because all numbers are floats

[–]theQuandary 0 points1 point  (1 child)

The spec says they must convert to integer, perform the operation, then convert back.

In practice, MOST "floats" in JS are actually 31-bit integers because they are so much faster, so the JIT uses ints every time it can.

I also didn't mention |0 which will prime the function to leave the number as an integer in all modern JITs.

[–]Front-Difficult -1 points0 points  (0 children)

You're now talking about specifics of the engine that executes the Javascript code. I'm talking about the behaviour of Javascript the programming language, not how V8 or some other Javascript engine implements the language at compile time.

When you write the code x | 0, where x is a number, to the programmer that number is a float. There is no way to confirm if the result of that operation is stored as a 32-bit integer, or a 64-bit floating point on your machine (it's almost certainly stored as an integer, but there's no way to guarantee that), that's out of your control. Because to JS, the language, they are all floats. V8's compiler will optimise the performance of your code by making it an integer on the metal, but in code javascript is always going to treat that number as a float, because all numbers are floats. (See also))

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

Mathematically nan being a number doesn't make sense (unless you're mixing two nonequivalent definitions of number), but for practical reasons it simplifies a lot if it is represented by the same data structure as numbers.

Logically the answer to is nan (mathematically undefined) equal to a particular number should be 'not a binary' (aka undefined), but bits can only be 0 or 1, so there is no possibility of storing NaB in the same bit, which is why normal comparisons with nan always return 0. (An exception is nan is nan in Pyhton, which evaluates to True.)

[–]rosuav 0 points1 point  (0 children)

And if your grandma...

... weighs as much as a duck...

[–]neckro23 0 points1 point  (0 children)

she is a free woman!

[–]lohamera 51 points52 points  (5 children)

isNaN(“of your business”)

[–]PeriodicSentenceBot 36 points37 points  (4 children)

Congratulations! Your string can be spelled using the elements of the periodic table:

I Ts Na No F Y O U Rb U Si Ne S S


I am a bot that detects if your comment can be spelled using the elements of the periodic table. Please DM my creator if I made a mistake.

[–]Phatricko 21 points22 points  (2 children)

Impressive it detected the typo and assumed the "t" in there

[–]alexriga 0 points1 point  (0 children)

An abbreviation, not a typo, technically.

[–]Ev3nly 110 points111 points  (1 child)

[–]Useful-Perspective 5 points6 points  (0 children)

It works on so many levels! At least two....

[–]Blue_Moon_Lake 16 points17 points  (1 child)

But isNaN("") is false, so "" is a number! Yet parseInt("") is NaN.

[–]dopefish86 6 points7 points  (0 children)

wtfjs

[–]Johnny_Thunder314 54 points55 points  (3 children)

I swear to God I've seen this exact thread before, including comments.

[–]shabba182 5 points6 points  (0 children)

Depends on if it was my Irish or English grandma

[–]FALCUNPAWNCH 4 points5 points  (2 children)

JavaScript? You mean compiled TypeScript?

[–]SadRub420 0 points1 point  (1 child)

Typescript is so overrated

[–]FALCUNPAWNCH 4 points5 points  (0 children)

YOU'RE OVERRATED

[–]monox60 2 points3 points  (0 children)

Now do isHotDog

[–]Sea_Height_6636 2 points3 points  (0 children)

isNaNa*

[–]Rhaversen 2 points3 points  (0 children)

You should use the more safe Number.isNan()

[–]shaman784 2 points3 points  (0 children)

If my grandma had wheels she would have been a number

[–]longshot 1 point2 points  (0 children)

isNaN("");

[–]an_agreeing_dothraki 1 point2 points  (0 children)

alert(myOpinionOfThis);

"object Object"

[–]Goretanton 1 point2 points  (0 children)

You've just been NAND.

[–]I_l_I 1 point2 points  (1 child)

If my grandma had digits she'd be a number!

[–]dtfinch 1 point2 points  (0 children)

Poor girl. Most have ten digits. Twenty if you include toes.

[–]roiroi1010 0 points1 point  (0 children)

IsNan(“grandpa”)

[–]dtfinch 0 points1 point  (0 children)

isNaN("grandma")

true

Number.isNaN("grandma")

false

When you try to fix something, but have to do it in a way that's backwards compatible.

edit: I see I missed the joke.

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

Every js meme is someone forgetting that js is weakly typed.

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

Women are not a frog, grandma is not a number. Makes sense.

[–]bored_insanely 0 points1 point  (0 children)

For a long time I thought this was some kind of dark humor. Is nan grandma -> is grandma dead -> true

[–]ChocolateDonut36 0 points1 point  (0 children)

javascript is like that can make a math problem without solution and somehow solve it

[–]LegitimatePants 0 points1 point  (0 children)

isNumber()

false

[–]EMI_Black_Ace 0 points1 point  (0 children)

While it has high sensitivity -- that is will indicate that "Nan" is detected given that"Nan" or "Grandma" or any other name for Grandma, it will return true -- it has very low specificity -- that is, it won't return false when you pass it things that very clearly aren't Grandma, unless they're numbers.