Why Do Empty JavaScript Arrays Evaluate to True in Conditional Structures

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!)

All other values are truthy, including "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:

  1. [] == true
  2. [] == 1
  3. "" == 1
  4. 0 == 1

Whereas based on operator precedence the ! 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:

Object: Return true.

Thus, when you supply any object to 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



Leave a reply



Submit