#Why is it that in JavaScript 2 + 2 = 22;, but 2 - 2 = 0;?

1 messages · Page 1 of 1 (latest)

stoic silo
#

This makes no sense lololol

woven tundra
#

string concatenation, if the two values are a string

stoic silo
#

Wait, so in JavaScript, integers are strings?

woven tundra
#

if not strictly typed they can be

#

"2" + 2 = 22 for instance

#

but 2 + 2 = 4

#

only for addition though, as the plus char is used for string concatenation, a minus not

stoic silo
#

Damn

#

I hope I don't have to learn JavaScript LOL

woven tundra
#

yeah use typescript instead

stoic silo
#

I don't like Microsoft lol

woven tundra
#

rip C#

stoic silo
#

Rip

#

I may use C++ instead

woven tundra
#

oof

stoic silo
#

Don't worry, I am very young, and I have enough time to learn pointers in C++

#

Though, I am currently learning regular C

#

I will have a blast when learning C++

woven tundra
#

regular C is just pure pain CattoCry

C++ is much better there, but still shit

stoic silo
#

regular C is just pure pain :CattoCry:
I like it

woven tundra
#

oh damn, never heard anyone say that. Even at my university people used to hate C

stoic silo
#

I haven't learned all of it yet, I can currently make very basic calculators, but I really like C

#

I am not a boomer or anything (actually the opposite), but so far I like the programming language

woven tundra
#

its good to have C as a skill though, theres that

fresh hedge
fresh hedge
grizzled juniperBOT
#

Why is 2 + 2 = 22 in JavaScript? - @stoic silo

Why is 2 + 2 = 22 in JavaScript?

2 + 2 = 22 in JavaScript is a result of a type coercion. JavaScript is a loosely typed language, which means that it will automatically convert different data types when necessary in order to complete an operation. In this case, when adding the number 2 and the number 2 together, JavaScript will convert the numbers to strings and concatenate them, resulting in the string "22".

velvet mango
#

js is so silly

stoic silo
#

"JavaScript is such an awful language" Stack overflow survey: Cope