Quantcast
Channel: Why do empty JavaScript arrays evaluate to true in conditional structures? - Stack Overflow
Browsing all 8 articles
Browse latest View live

Answer by Abir for Why do empty JavaScript arrays evaluate to true in...

[]==false // returns trueThis evaluates to true, because of the Abstract Equality Algorithm as mentioned here in the ECMA Specification #Section 11.9.3If you run through algorithm, mentioned above.In...

View Article



Answer by Max Senisch for Why do empty JavaScript arrays evaluate to true in...

I suspect it has something to do with discrete math and the way a conditional (if then) works. A conditional has two parts, but in the instance where the first part doesn't exist, regardless of the...

View Article

Answer by Shivam Sharma for Why do empty JavaScript arrays evaluate to true...

As in JavaScript, everything is an object so for falsy and empty, I use the below condition:if(value && Object.keys(value).length){ // Not falsy and not empty}else{ // falsy or empty array/object}

View Article

Answer by ZloiGoroh for Why do empty JavaScript arrays evaluate to true in...

Also want to add, that all objects in JavaScript (arrays are objects too) are stored in memory as links and these links are always not null or zero, that's why Boolean({}) === true, Boolean([]) ===...

View Article

Answer by bvdb for Why do empty JavaScript arrays evaluate to true in...

While [] equals false, it evaluates to true.yes, this sounds bad or at least a bit confusing. Take a look at this:const arr = [];if (arr) console.log("[] is truethy");if (arr == false)...

View Article


Answer by Barmar for Why do empty JavaScript arrays evaluate to true in...

From http://www.sitepoint.com/javascript-truthy-falsy/The following values are always falsy:false0 (zero)0n (BigInt zero)"" (empty string)nullundefinedNaN (a special Number value meaning...

View Article

Answer by DevlshOne for Why do empty JavaScript arrays evaluate to true in...

You should be checking the .length of that array to see if it contains any elements.if (myCollection) // always trueif (myCollection.length) // always true when array has elementsif...

View Article

Why do empty JavaScript arrays evaluate to true in conditional structures?

I was encountering a lot of bugs in my code because I expected this expression:Boolean([]); to evaluate to false.But this wasn't the case as it evaluated to true.Therefore, functions that possibly...

View Article

Browsing all 8 articles
Browse latest View live


Latest Images