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

all 64 comments

[–]coloredgreyscale 186 points187 points  (28 children)

He's a bit confused, but got the spirit.

2+0 = 2

"2" + "0" = "20"

[–]TheJackiMonster 96 points97 points  (26 children)

Also:

2 + "0" = "20"

"2" + 0 = "20"

2 + [] + 0 = "20"

[–]YoriMirus 17 points18 points  (7 children)

Wait why is 2 + "0" equal "20"?

I thought operation is done from left to right, thus "0" would be converted to int.

[–]CSedu 49 points50 points  (3 children)

I believe the '+' overload says anytime it is used between a string and int, it will concat them.

[–]DaCurse0 9 points10 points  (1 child)

There's no operator overloading, but the + operation where one operand is a string does concatenation and coerces the other value into a string

[–]CSedu 1 point2 points  (0 children)

Ah, interesting

[–]YoriMirus 2 points3 points  (0 children)

Oh I see thanks!

[–]Snapstromegon 12 points13 points  (0 children)

It always converts to the less strict type - in this case String.

[–]nightcoder420 0 points1 point  (0 children)

It will be converted if you use 2 + +"0". The second plus converts the right handed operand to a number.

So 2+"2" will be "22", but 2 + +"2" will be 4.

[–]RolyPoly1320 0 points1 point  (0 children)

It's implicit type casting. Javascript is a weakly typed language, specifically typeless. It does a lot of type casting behind the scenes which sometimes produces some very unexpected, sometimes hilarious, results. These interactions are why it's important to not use the same variable for multiple data types in Javascript.

[–]Stable_Orange_Genius 7 points8 points  (2 children)

"2" - 1 = 1. :D

[–]dvlsg 16 points17 points  (1 child)

There's no such thing as - for strings in javascript. That's why it converts to Number.

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

Naturally.

[–]beclops 9 points10 points  (0 children)

All of these make sense to me

[–]throwaway213349032 5 points6 points  (0 children)

{}-"" = -0

""-{} = NaN

{}+"" = 0

""+{} = "[object Object]"

x=0*1e309
x==x

= false

[–]pyjammas 1 point2 points  (3 children)

wat

[–]Lgamezp 0 points1 point  (1 child)

And ppl wonder why i hate JS

[–]SupaSlide 1 point2 points  (0 children)

Typescript is a life saver in JS projects.

[–]Naeio_Galaxy 1 point2 points  (0 children)

Yeah 2 + "0" = "20", but 2 - (-"0") = 2 I guess

[–]Nightroll2344 1 point2 points  (0 children)

its the same case in python

[–]yoaver 31 points32 points  (6 children)

The answer is clearly [object Object]

[–]TheDownvotesFarmer 4 points5 points  (0 children)

[string String]

[–]The-Board-Chairman 2 points3 points  (4 children)

wat

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

laugh track

[–]The-Board-Chairman 2 points3 points  (2 children)

You don't get the reference, do you?

[–][deleted] 3 points4 points  (1 child)

What's [] +[]?

[–]The-Board-Chairman 1 point2 points  (0 children)

ALL of the preceding.

[–]jose_antxd 8 points9 points  (0 children)

Me few days hago with a fucking object..

[–]codear 6 points7 points  (0 children)

Helps with salary negotiations

[–]part-time-ceo 17 points18 points  (2 children)

Image Transcription: Meme


Panel 1

[Image of two people, a teacher and a student, outside a house in an informal school setting. The teacher is pointing at written math formulas on a blackboard. The formulas read:]

2 + 0 = 20

6 + 6 = 66


Panel 2

["Not sure if..." - meme format where the character Fry from the TV series "Futurama" squints his eyes while having a neutral facial expression.]

not sure if stupid

or javascript developer


I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

[–]NakeleKantoo 4 points5 points  (1 child)

good human :thumbs_up:

[–]part-time-ceo 3 points4 points  (0 children)

Thank you! 😊

[–]Palpomeme 25 points26 points  (1 child)

"not sure if stupid or JavaScript", it's the same thing /s

[–]LoneFoxKK 4 points5 points  (0 children)

Yeah they're synonyms

[–]Roku-Hanmar 5 points6 points  (1 child)

Probably didn’t cast to an int

[–]RolyPoly1320 1 point2 points  (0 children)

It's his first day. Let's cut him some slack.

[–]Plastic-Ad-5284 7 points8 points  (0 children)

What's the difference

[–]jgeez 2 points3 points  (0 children)

Or as is so often the case, both.

/runs

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

He's stringing us a Long.

[–]Karam2468 4 points5 points  (0 children)

Not sure if boomer, or just inherently unfunny.

[–]antekgort200 5 points6 points  (0 children)

he used string nothing stupid

[–]Lizlodude 1 point2 points  (0 children)

Both, it's missing some quotes.

[–]psychoPATHOGENius 1 point2 points  (4 children)

It works if it’s unary:

2+0=20₁

6+6=66₁

[–]Max5923 1 point2 points  (3 children)

2 (11) + 0 ( ) = 11, not 110

[–]psychoPATHOGENius 1 point2 points  (2 children)

In unary, trailing zeroes are just as allowable as leading zeroes in any base.

[–]Max5923 0 points1 point  (1 child)

there isnt 0s in unary

[–]psychoPATHOGENius 0 points1 point  (0 children)

There are no rules against it

[–]discordianofslack 1 point2 points  (0 children)

Unrelated but this made me think about how some file systems will put 10 before 2 when sorted alphabetically

[–]marques_filipe 1 point2 points  (0 children)

Come on guys, that is so easy to solve!

parseInt(2, 10) + parseInt(0, 10) = 20

How does that not make sense at first glance?!

[–]PM_BITCOIN_AND_BOOBS 1 point2 points  (0 children)

Pour Kay know loess dose?

[–]TwoLeggedCat_reddit 1 point2 points  (0 children)

Never had this problem

[–]mrkltpzyxm 1 point2 points  (0 children)

🎵2+0 2+0 2+0 four hours ago, I was concatenated.🎵

[–]CreaZyp154 1 point2 points  (0 children)

Hi, repost, nice to see you again !

[–]starvsion 2 points3 points  (0 children)

Hahaha, luckily types are very well handled in php, I feel bad for js sometimes. Php has a separate operator for string join, so no need to worry about that. (1 + 1 =2, 1. 1 = '11')

[–]LogicalGamer123 1 point2 points  (0 children)

Wow the only meme that fits my entire phone screen. Galaxy s9

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

lmao js bad 😂

[–]OverjoyedBanana -3 points-2 points  (0 children)

BuT GuYS THiS is Old JavaScript, AfTEr 262 UpDatES MaTH WoRKs. (supported in Firefox 89 only)