Did you know this about JavaScript?

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

JavaScript is one of those that skipped math class

fredroloji
Автор

typescript : i still remember old days when i was a naughty boy.

ihateorangecat
Автор

Clearly a feature, just like {} + [] equaling 0

cbspzhz
Автор

This is called "Type Coercion".

darkpain
Автор

If we list all JS strange things we would need kilometers of paper

ezkymos
Автор

JavaScript: king of unexpected behaviour

joofville
Автор

The character subtraction technically works in the same way in c since the ascii 3 is represented by number 51 and ascii 1 is represented by number 49 and 51-49 is obviously 2.

asdprogram
Автор

You can remember this easily. Js can add strings but it cannot subtract strings, so it converts it into number and do the operation. If it cannot convert it into a number while subtracting, it will return NaN.

mprasanth
Автор

Javascript is a toxic partner.. :) I’ve been in love/hate relationship with it for 2 decades 😂 ❤ 💔😭

marjanek
Автор

Can you use 2 minuses to add the values of the strings?

gamesandstuff
Автор

It’s not that strange, + is overloaded for string concatenation while - is not, so it attempts to coerce the string arguments to numbers.

The quirk is that strings are coerced to numbers at all, I didn’t even know javascript did that. I hate this language

minnow
Автор

Ever tried to convert a single digit char to the number it self in C?

lynhrt
Автор

+ has two roles add and concat 🤷 but - has only one substract not weird at all

csangale
Автор

How to make a developer's life hell 101 - Javascript

naveenvenkateshk
Автор

32 bcuz js consider it as concatenatio if u use + sign to add to strings, 2 bcuz js converts it into number before executing the - operation, note: it only works with the - sign so if u use * or / js will concatenate both string and it will not convert them this is base on what i remember. :)

louisdanielmarino
Автор

Feels like type coercion is the cool party trick to show people who are not familiar with Javascript.

claudiu
Автор

For character, the plus operator is used for concatenation. The subtraction works this way because the ascii numbers of therespective characters are used. Nothing unexpected or weird imho

flyingmindzofficial
Автор

I know this it checks if both are numeric values and give an answer

shubhranshbhardwaj
Автор

This is because the plus sign is used as bot string concatenation and number addition. On the other hand, minus is only used as number subtraction.

aprilmintacpineda
Автор

I was also curious if it converted it back to string or left it as number. The result is actually a number.

ShiloBuff