0

In JavaScript:

var stringVar = 12345 + []

Will cast the number to a string.

Small demo: https://jsfiddle.net/ce9bjcwo/

Why does this happen?

George Kagan
  • 5,207
  • 8
  • 44
  • 49
Alberto Rivera
  • 3,127
  • 3
  • 14
  • 29

3 Answers3

1

Due to Array.prototype.toString().
It converts it to a string representation when trying to concatenate, and since 12345 is not a string, it's type-cast to one.

Examples:
[1,2].toString() -> "1,2"
[].toString() -> ""
12345 + "" -> "12345"

George Kagan
  • 5,207
  • 8
  • 44
  • 49
1

It happens because:

There is no way to apply the + operator to an array, so Javascript stringifies the array. Since it's an empty array, it stringifies as ''. Then you have 12345 + ''

And in javascirpt "number" + "string" returns a string, by casting the number to a string and treating + as concatenation operator.

Mark Adelsberger
  • 32,904
  • 2
  • 24
  • 41
1

it's due to the fact it is trying to concatenate it to an empty string You may understand it better if you try something like

  var stringVar = ""+123;

the amazing thing is that if you do

  stringVar = stringVar - 0 ;

it turns to an integer

then if you evaluate

stringVar == '123'

you get true

but if you evaluate

stringVar === '123'

you get false

so cool

Andrea Sindico
  • 7,058
  • 6
  • 41
  • 79
  • It's not really _that_ amazing - the `+` operator is just doing too much stuff - maths AND concatenation, which forces it to have to decide on the fly which operation to apply and hence the type coercion. The `-` one is doing only mathematical operations, so it always returns a numeric type. But I'm not really sure that part of the answer really _answers_ the question. – VLAZ Nov 04 '16 at 21:06
  • @vlaz The `+` operator is actually doing just one thing, addition. It's just that there are two kinds of addition in programming: mathematical and string. – Scott Marcus Nov 04 '16 at 21:11
  • @ScottMarcus string concatenation is NOT addition. Some languages treat it as "the same" by overloading the `+` operator, however, PHP and Perl, for example, the concatenation operator is _different_ and is `.`, so you will have to know what you're doing: `"hello" . "world"` is the thing that will produce a new string. There are many other types of "addition" in programming that _could_ be handled by `+` but aren't because it will lead to weird overload cases. – VLAZ Nov 04 '16 at 21:15
  • @vlaz Actually string concatenation IS addition. It's string addition which results in concatenation. This concept is the same in all languages however, some languages allow the developer to explicitly choose which variant they want with a dedicated operator. JavaScript does not - it uses an overload instead. – Scott Marcus Nov 04 '16 at 21:20
  • @ScottMarcus—the `+` punctuator is interpreted as one of two operators. The [*addition operator*](http://www.ecma-international.org/ecma-262/6.0/#sec-addition-operator-plus) which results in either mathematic addition (returning a Number) or string concatenation (returning in a String). String concatenation is a distinctly different operation from numeric addition. `+` can also be interpreted as the [*unary + operator*](http://www.ecma-international.org/ecma-262/6.0/#sec-unary-plus-operator) which does type conversion to Number as for *ToNumber*, so it performs 3 different operations. – RobG Nov 04 '16 at 23:26