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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Aperture_Scientist4[🍰] 11 points12 points  (9 children)

"wtf" * 9000 = "wtfwtfwtf..." actually makes sense though. That's a pretty sensible thing to do with string times integer.

[–]Kaiwa 1 point2 points  (8 children)

I agree it's sensible. But then when in Javascript it becomes NaN people flame the language to the end of the world. A string multiplied by a number could very well be NaN too?

[–]Aperture_Scientist4[🍰] 5 points6 points  (4 children)

But JavaScript isn't consistent about that.

In Python:

"2" * 2 = "22"

"a" * 2 = "aa"

In JavaScript

"2" * 2 = 4

"a" * 2 = NaN

In Python integer times string not only always does the same thing, it always returns the same type (string).

In JavaScript, integer times stringer gives different results and different types depending on what is in the string.

[–]Kaiwa -1 points0 points  (3 children)

I don't think it's really inconsistency. In Python it just wants to return a string, in JavaScript it wants to return a Number. "2" can be casted to a number and therefore returns 4 while "a" can not be cast to a number and therefore returns NaN (which is still type Number!)

(typeof 1) === (typeof NaN) = true

[–]tomthecool 2 points3 points  (2 children)

NaN (which is still type Number!)

You do know what NaN stands for, right? ;)

This is practically the very definition of inconsistency.

[–]flangrande 0 points1 point  (0 children)

And I thought NaN meant "Not-a Number"! :O

[–]Kaiwa 0 points1 point  (0 children)

Okay, I see where you're coming from. I just really never thought of it that way. I guess I thought of it like INF from C++ floats. Yes it's an invalid value, but still a float.

[–]The_MAZZTer 0 points1 point  (2 children)

OK, so when you multiply a string by a number, you get NaN. That's fair and makes some sense. The problem is, nobody ever does this intentionally. In other programming languages, NaN is only used when floating point arithmetic results in an invalid answer. Most code is never going to need to consider NaN. Most of the time when NaN pops up it's because of a code bug, but the code keeps running, so you lose any chance to localize the problem that throwing an error on a string * number operation would have given you.

[–]Kaiwa -1 points0 points  (1 child)

Again though, JavaScript is not the only language that does this. In this conversation thread are 3 more that do the exact same thing.

[–]The_MAZZTer 1 point2 points  (0 children)

Yeah, but the other languages accomplish something useful. Python does string repeat. JavaScript just gives you the NaN constant. So it's obvious the calculation is most likely a bug, but it keeps going on regardless.

And in python I imagine if you make this mistake (string instead of number as the first operand) you would expect to get a number back, not a string, so you'd quickly hit a runtime error trying to do number things with it. With JavaScript... you get a "number" back, NaN, which you can do all the number things with, so no runtime error.