Jennifer Aniston
Jennifer Aniston

Reputation: 613

null escaped in nodejs

I console.log("var = " + JSON.stringify(result.something));

I got var = null

but when I do

if(result.something !=null || result.something != ''){
console.log('enter')
}

it print enter also. I wonder why is that happening, I also tried result.something != 'null', it still go into the if statement.

Upvotes: 0

Views: 202

Answers (2)

riser101
riser101

Reputation: 650

Your variable is null, here's why:

 1. (result.something !=null) : returns false

 2. (result.something != '')  : returns true

Since you've used an OR operator, program control is going to go inside the if block if either of the condition is true.

As your 2nd condition is evaluating to be true, it's going inside of the if block.

From javascript MDN:

null : "an empty value" i.e no object value present

null value is different from an empty string. So something like if(null ==== " ") will return false

Upvotes: 1

aifarfa
aifarfa

Reputation: 3939

your if statement always true because

the result.something is null AND it is not an empty string null != ''

:)

Upvotes: 0

Related Questions