admin管理员组文章数量:1306835
Why does an empty array plus false return the string "false"?
> [] + false
> "false"
An empty array is false, right?
Then false + false = false? No?
Why does an empty array plus false return the string "false"?
> [] + false
> "false"
An empty array is false, right?
Then false + false = false? No?
Share Improve this question edited Oct 19, 2019 at 19:09 Boann 50.1k16 gold badges124 silver badges152 bronze badges asked Oct 19, 2019 at 17:37 POVPOV 12k39 gold badges128 silver badges229 bronze badges 8-
7
empty array -> toString() ->
''
-> plus something -> string as result. – Nina Scholz Commented Oct 19, 2019 at 17:39 -
@NinaScholz This does not explain why there's a
.toString()
call in the first place. – Andreas Commented Oct 19, 2019 at 17:40 -
Operator
+
casts tostring
ornumber
(whatever is possible, in this order). It doesn't cast toBoolean
. – goodvibration Commented Oct 19, 2019 at 17:41 - right, it is the plus sign, which triggers the operands to a usable form. – Nina Scholz Commented Oct 19, 2019 at 17:42
- 2 Would this work as a suitable dupe target? What exactly is Type Coercion in Javascript? – Andreas Commented Oct 19, 2019 at 17:43
4 Answers
Reset to default 8Short answer: because the spec says so.
Longer answer: When the left operand of the +
operator is not a string or a number, the operand will be converted to either one of those depending on their "preferred" primitive type (defined in the spec). The array's "preferred" type is a string and [].toString()
is an empty string.
Then, still according to the spec, because the left operand is a string (following the previous conversion), the right operand will be converted to a string and the result of the +
operation will be the concatenation of both strings.
In other words, [] + false
is equivalent to [].toString() + false.toString()
(or "" + "false"
) and results in the string "false"
.
Other interesting results as a consequence of this:
[1,2,3] + false // "1,2,3false"
[1,[2]] + false // "1,2false"
[] + {} // "[object Object]"
The '+' operator casts to string
or number
(whatever is possible).
It doesn't cast to Boolean
, check it out for yourself:
const x = [] + false;
const y = false + false;
console.log(typeof(x)); // string
console.log(typeof(y)); // number
The general rule for addition in JavaScript is simple: You can only add numbers and strings, all other values will be converted to either one of those types.
Source: https://2ality./2012/11/coercing-objects.html
First the +
operator must make sure that it operates on primitives (either numbers or strings). If an operand isn't a primitive it must first convert it.
Here are the rules:
- If the operand is a primitive then stop.
- Otherwise call its
.valueOf()
method. If the return value is a primitive then stop. - Otherwise call its
.toString()
method.
In [] + false
, []
isn't a primitive. Let's convert it:
([]).valueOf(); //=> [] (not a primitive)
([]).toString(); //=> "" (primitive)
We now have "" + false
to solve.
If any operand is a string, then +
returns the concatenation of both operands. Meaning that false
must be converted to a string:
(false).toString(); //=> "false"
The final operation bees "" + "false"
which returns "false"
.
What does false + false
return?
Both operands are primitives and both aren't strings. So they must both be converted to numbers.
Number(false); //=> 0
+false; //=> 0
Therefore false + false
bees 0 + 0
which returns 0
.
How to make [] + false
returns 42
?
Simple:
- Force
[]
to return42
42 + false
will causefalse
to be coerced into a number:0
42 + 0
returns42
Example:
var empty_arr = [];
empty_arr.valueOf = () => 42;
console.log(empty_arr + false);
Or: (but that's nasty)
Array.prototype.valueOf = () => 42;
console.log([] + false);
Basically it's because JS is trying to concat an array and then adds the string. Take a look on this:
[1] + true // "1true"
[2] + 5 // "25"
[2, 3, 4] + 5 // "2,3,45"
Similar to:
"".concat([3, 2, 1]); // "3,2,1"
More info: Why is [1,2] + [3,4] = "1,23,4" in JavaScript?
本文标签: javascriptWhy does an empty array plus false in JS return a stringStack Overflow
版权声明:本文标题:javascript - Why does an empty array plus false in JS return a string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741801257a2398239.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论