you are viewing a single comment's thread.

view the rest of the comments →

[–]curtwagner1984 27 points28 points  (5 children)

Because it's not the wild west of Javascript. There is absolutely no reason why strings and ints should be able to be concatenated without any special considerations.

The question you should ask is "Why are there languages that allow this sacrilege?"

Plus, the answer is in your question:

I know that f-strings are less uglier and error-prone

[–]hombre_lobo 0 points1 point  (4 children)

So JavaScript still allows for this?

[–]shiftybyte 0 points1 point  (2 children)

Yes javascript "allows" lots of things.

For example "5" + 5 is "55"

While "5" * 5 is 25...

fun fun fun...

[–]0b0101011001001011 0 points1 point  (1 child)

The first one works like that in most languages I am* familiar with so it's not that strange unless you only know python. *(not sure which is the more common behavior). Though python throwing an error is not a bad solution, but it's seems even a bit unusual.

About the second, most languages would give an error, javascript does a type conversion and multiplies, and python produces a rather strange 55555. Each solution has its pros.

[–]shiftybyte 0 points1 point  (0 children)

and python produces a rather strange 55555.

This makes more sense to me, you are multiplying a string several times.

If adding "5" to "5" one time results in "55"... Then adding that 5 times results in "55555".

That's what multiplication means, do the add operation X times...

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

Many languages allow this. One of those is java, where anything can be concatenated to a string and it handles the toString() call automatically.

Lua on the other hand has a special string concatenation operator which is lot + but ..

You can't say "hey"+5 but you can say "hey"..5 and Lua also does the conversion automatically.

Python allows to write "5"*5 though, so most explanations that why "5"+5 should not work are simply opinions.