admin管理员组文章数量:1316356
In my code I assumed the following ||
short-circuiting was safe:
var $holidayExpandBarOrOpeningHours =
$(".expandBar + .holidayHours_c").prev() || $(".openingHours");
But to my surprise if we short-circuit an empty array with a true statement an empty array is still returned. I will demonstrate with some console code below and my question is why [] || true
evaluates to []
.
false || "expected"
"expected"
false == []
true
[] || "expected"
[]
typeof([])
"object"
({}) || "expected"
Object {}
({}) == false
false
{} == false
SyntaxError: Unexpected token ==
Part of me thinks that it is because an array is an object
which evaluates to true, however if that was the case than based on ({}) == true
one would expect [] == true
.
Last thing I would like to note is the oute is the same when using use 'strict'
mode.
In my code I assumed the following ||
short-circuiting was safe:
var $holidayExpandBarOrOpeningHours =
$(".expandBar + .holidayHours_c").prev() || $(".openingHours");
But to my surprise if we short-circuit an empty array with a true statement an empty array is still returned. I will demonstrate with some console code below and my question is why [] || true
evaluates to []
.
false || "expected"
"expected"
false == []
true
[] || "expected"
[]
typeof([])
"object"
({}) || "expected"
Object {}
({}) == false
false
{} == false
SyntaxError: Unexpected token ==
Part of me thinks that it is because an array is an object
which evaluates to true, however if that was the case than based on ({}) == true
one would expect [] == true
.
Last thing I would like to note is the oute is the same when using use 'strict'
mode.
- 2 An empty array is not falsy – rgthree Commented Dec 18, 2013 at 21:34
- In support of what @rgthree said, this article has a simple write-up about truthy/falsy values that you may be interested in. – War10ck Commented Dec 18, 2013 at 21:37
-
({}) == true
is false, just as[] == true
is false. The process is the same. – user2864740 Commented Dec 18, 2013 at 22:32
3 Answers
Reset to default 6When converted to a boolean value, []
is true.
> !![]
true
> ![]
false
When converted to a number, []
is 0. That's why paring it with false returns true: when paring two values of different types, JavaScript first converts both to numbers and then pares the numbers.
> +[]
0
> +false
0
> +[] == +false
true
This is because ||
and use ==
different rules for the conversion.
The logical-or uses ToBoolean
while the equality equals uses ToNumber
/ToPrimitive
.
From 11.11 Binary Logical Operators:
3) If ToBoolean(lval) is true, return lval.
Since ToBoolean([])
is true, [] || x
results in []
. This is also why if([]) { /* this runs */ }
: arrays in JavaScript are "truthy" values.
From 11.9.3 The Abstract Equality Comparison Algorithm:
7) If Type(y) is Boolean, return the result of the parison x == ToNumber(y).
9) [..then] If Type(x) is Object and Type(y) is either String or Number, return the result of the parison ToPrimitive(x) == y.
5) [..then] If Type(x) is String and Type(y) is Number, return the result of the parison ToNumber(x) == y.
The logic applied to [] == true
is ToNumber(ToPrimitive([])) == ToNumber(true)
.
And the conversion values:
ToBoolean([])
is trueToNumber(true)
is 1ToPrimitive([])
is an empty string (fromDefaultValue/toString
)
So:
ToNumber(ToPrimitive([])) == ToNumber(true)
ToNumber("") == 1
0 == 1
false
(Which is a long way to say what John Kugelman said.)
Also, ({}) == true
is normally false and follows the same conversions as above.
Under a default environment, ToPrimtive({})
returns a non-empty string (i.e. "[object Object]"
as per Object.prototype.toString). This string will evaluate to NaN after ToNumber
such that NaN == 1
; or false.
See Why an empty Array type-converts to zero? +[] for more details on the ToPrimitive conversion.
An empty array is an object; objects coerced to booleans are true
. So
({}) || true; // -> {}
[] || true; // -> []
"" || true; // -> true (empty strings are coerced to false)
Sidenote - the parentheses around {}
are required to avoid parsing it as a block.
本文标签: javascriptShortcircuiting an empty array in JS has an unexpected outcome trueStack Overflow
版权声明:本文标题:javascript - Short-circuiting an empty array in JS has an unexpected outcome: `[] || true == []` - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742002969a2411387.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论