Khanakia
Khanakia

Reputation: 742

Node.js Reacting weird in IF Condition

I have function to check if key exists or not so the below if i do console.log it returns null not true||false on Live server but on my MAC OSX local it works file

 console.log((key=='parent' && atts.filters[key]))  //returns null not true||false
 if(key=='parent' && atts.filters[key]) {         
    atts.filters[key] = 'demo'
 }

So can anybody helps to figure it out what wrong here ?

Upvotes: 0

Views: 74

Answers (2)

Mamun
Mamun

Reputation: 68933

Logical operators

Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

If atts.filters[key] returns null or empty string, you can use Boolean to convert the value:

var key = 'parent';
console.log((key=='parent' && Boolean(null)))
console.log((key=='parent' && Boolean('')))

Upvotes: 1

Biswadev
Biswadev

Reputation: 1486

atts.filters[key] might be returning null

console.log(true && null) //null

Upvotes: 0

Related Questions