Following values are always false
false
0
"" (empty string)
null
undefined
NaN
Following values are true
"0" - zero within string
"false"
empty arrays
empty functions
empty objects
function test(){
let validations = {"wrongParams":[], "missingParams":[]};
if(validations.missingParams) {
console.log('hi');
}
else{
console.log("hello");
}
}
Above program prints "hi" because it is evaluating empty array.
Why?
if (arr) called on object (Array is instance of object in Javascript) will check if the object is present and returns true/false.
When you call if(arr === false) you compare values of this object and the primitive false value. Internally arr.toString gets called which returns an empty string "". Emptry string is one of falsy values in Javascript.