Why do empty JavaScript arrays evaluate to true in conditional structures?
From http://www.sitepoint.com/javascript-truthy-falsy/
The following values are always falsy:
- false
- 0 (zero)
- 0n (BigInt zero)
- "" (empty string)
- null
- undefined
- NaN (a special Number value meaning Not-a-Number!)
"0"
(zero in quotes), "false"
(false in quotes), empty functions, empty arrays ([]
), and empty objects ({}
).Regarding why this is so, I suspect it's because JavaScript arrays are just a particular type of object. Treating arrays specially would require extra overhead to test Array.isArray()
. Also, it would probably be confusing if true arrays behaved differently from other array-like objects in this context, while making all array-like objects behave the same would be even more expensive.
Why do both [] == true and ![] == true evaluate to false?
It's the way coercion works.
The first step of coercion is to convert any non primitive types to primitive types, then using a set of rules convert the left, right or both sides to the same type. You can find these rules here.
In your case [] == true
, would pass through these 4 steps:
[] == true
[] == 1
"" == 1
0 == 1
!
in ![] == true
is executed first so the expression is converted to false == true
which is obviously false
.You can try the live demo by Felix Kling to better understand how the sameness operator works.
In more words:
The value ![]
is false
, because []
is an Object (arrays are objects) and all objects, not including null
, are truthy. So any array, even if it is empty will always be a truthy, and the opposite of a truthy is always false
. The easiest way to check if a value is a truthy is by using !!
.
console.log("![]: " + ![]);
console.log("!![]: " + !![]);
Why is Boolean([]) true in JavaScript?
The ECMAScript specification defines how values are cast to booleans, per the abstract ToBoolean
operation: https://www.ecma-international.org/ecma-262/6.0/#sec-toboolean
That operations includes a single entry for object input:
Thus, when you supply any object toObject: Return
true
.
Boolean
, including an array (even an empty one), you'll get a true
value back, Javascript: testing for at least one non-empty array (if one of the arrays may be null)
I would suggest using a helper function to determine if a single array is non-empty, and then use that twice. This is simple and straightforward:
function isNonEmptyArray(arr) {
return !!(Array.isArray(arr) && arr.length);
}
var myBooleanAnd = isNonEmptyArray(myArray1) && isNonEmptyArray(myArray2);
var myBooleanOr = isNonEmptyArray(myArray1) || isNonEmptyArray(myArray2);
why does empty conditional statement for java for loop evaluate to true?
A conditional statement must be of boolean
type in order to control looping, in exactly the same way that the expression within an if()
or a while()
statement must be of boolean
type.
The only valid values for boolean
are true
and false
. So, null
and void
are not applicable, they do not make any sense.
As for empty conditional statements, of the two possible values true
and false
, they chose true
because there is absolutely never any need for a loop that will not loop at all, while there are plenty of situations where we have a need for a loop that will loop forever (or until break
.)
Related Topics
How to Bind Bootstrap Popover on Dynamic Elements
How to Detect When a Tab Is Focused or Not in Chrome with JavaScript
Cannot Set Property 'Innerhtml' of Null
Convert Unix Timestamp to Date Time (Javascript)
Why Is There a 'Null' Value in JavaScript
Angularjs - Cancel Route Change Event
Why Does This Foreach Return Undefined When Using a Return Statement
Saving a Text File on Server Using JavaScript
Single Quote Escape in JavaScript Function Parameters
How to Remove Text (Without Removing Inner Elements) from a Parent Element Using Jquery
What Causes the Error "Can't Execute Code from a Freed Script"
Click Everywhere But Here Event
What's Wrong with Var X = New Array();
Scope of Default Function Parameters in JavaScript
How to Load a JSON Object from a File with Ajax
How to Find a Reason Angularjs "Argument 'Myctrl' Is Not a Function, Got Undefined"