Accidental string concatenation in JavaScript #javascript

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

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

When people want to complain about JS and this is their only argument agains't it 😂

setasan
Автор

This makes a lot of sense. The alternative would be to check every string for numbers. If you know your string is only gonna contain numbers you should explicitly parse the string to a number.

I believe this is the same behavior in C# which I use on a daily basis.

jackoberto
Автор

I wouldn't blame javascript for your poor typing. Adding a string to a number SHOULD concat them, because thats the only valid thing you'd be doing with a number result and a string.

In C it'd just add the ASCII value of the character to the number and that would also be nonsense.

Ideally youd just convert the string yourself and somehow stop invalid input. Because no automatic language level solution would be good for all situations.

WIImotionmasher
Автор

This is the expected behavior, you can always prefix the variable with a + to convert it to number (just to be safe), or use zod/joi/yup to validate.
I don't think this is a deal breaker for JS, if not just shows how far nitpicking people that don't like JS goes to get their argument.

eliseuvideira
Автор

This makes perfect sense. Unless you want it to throw an error, this is desirable behaviour

Starman
Автор

This is a standard issue of all dynamically typed programming languages. Every language designer has to decide what to do in such a case. JS does type coercion. Maxbe if Bendan Eich had more than 10 days to create the language he would have decided this is a bad idea. 💀

teckyify
Автор

Js is made for web development where string concatenate is more required and more safe to convert number to string than string to number .

ArabPhalastine
Автор

This is perfectly fine not a problem of javascript but the way you have been doing things.

muhammadbilal
Автор

All other languages do not have to deal with the DOM

hcmlopes
Автор

Almost all languages work like this for plus: C#, C++, Java.

QwDragon
Автор

I watched this like 3 times and didn’t even realize it was just looping 😭😭

rupturepk
Автор

Bruh i almost spent my entire life watching this

malesnugas
Автор

Other languages, just do not compile. And that is that.

keybraker
Автор

In most programming languages the program would have crashed, even in python.

Perhaps if it was a number input it would format the value as an int or float

erictheawesomest
Автор

That's why you should use typescript

davronmaxmudov
Автор

It may seem obvious what you need with this instruction but for javacript it just picks whatever comes first. Javascript cannot tell with your instruction if you want 21 or 156. This is only a direct result of javascript not being strongly typed. TypeCoercion, aka, implicit conversion, is the only way javascript can arbitrarily guess what the desired result is. This happens I believe in some weakly typed or non-typed languages like javascript. You’d find type erasure in strongly typed jvm languages also awful for the similar reasons. Typescript is something else but also has its quirks. I agree that it is difficult to program this way, but that’s how the paradigms for javascript are implemented.

jesprotech
Автор

I think javascript needs to do something about errors like there are so many functions that throw an error and it doesn't tell you.

latertheidiot
Автор

To be fair, if I know the field is supposed to be numbers only I use ++. Type coercion works too

hikari
Автор

Like you said this is JavaScript!! And by the way sometimes we need some dirty code to save the day and only JavaScript is capable of this type of work!

MoBalic
Автор

JavaScript treating an addition involving a string as concatenation is good. It is dangerous to allow implicit conversions from a string to a number as the behaviour of the code will fundamentally change depending on what the contents of the string is. The problem isn't the concatenation but how values from a number tag don't come as a numeric type to begin with.

HTownsend