I don't understand why an empty array, or an array with only 1 "numerical" value can be used in certain calculations.

```
[] * [] === 0 //true

[2] * [2] === 4 //true

["2"] * ["2"] === 4 //true

```

However, it does not seem that is always the case with every operator.

```
[2] + [1] === 3 // false, actual result is "21"

[2] - [1] === 1 // true

```

I also checked these, and saw expected results:

```
[4] === "4" // false
[4] === 4 // false

```

In most cases, I would expect `NaN` in any of these mathematical operations. What is it about JavaScript arrays that allows this? Is it because the array `toString` method is being used somehow internally?
['https://Stackoverflow.com/questions/36656845', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2056157/']
The arrays are coerced into strings, such that `[] == ""`, `[1] == "1"`, and `[1, 2] == "1,2"`.

When you do certain mathematical operations on strings, they are coerced into `Number` types.

For example, when you do `[2] * [2]` it becomes `"2" * "2"` which becomes `2 * 2`. You can even mix types and do `[2] * 2` or `"2" * [2]`.

When you try to use `+`, it attempts to concatenate the strings instead of coercing the strings to numbers. That's why `"2" + "2"`, `"2" + [2]`, and `[2] + [2]` each give you `"22"`.
The same ol'problem of having `+` as string concatenation operator.

An array in Javascript is an Object. When you try to coerce objects into primitive values, there is an order to follow:

1. `<obj>.toString()`
2. `<obj>.toNumber()`
3. `<obj>.toBoolean()`

If you're using the `*` operator, coerce into string is not possible, so the interpreter goes to `toNumber()`.

When you're using the `+` operator, the first attempt will be `toString()`. Since `+` is also the string concat operator, then the expression will be evaluated as an array.

Yeah, [javascript](/questions/tagged/javascript "show questions tagged 'javascript'") can get ugly.

Edit:
-----

Some further details can be found [here](https://www.safaribooksonline.com/library/view/you-dont-know/9781491905159/ch04.html).