if condition with ternary operator not returning true or false

namko :

I am comparing 2 object properties in Nodejs

 if(alteredItem.main != (result.main)?result.main:"NULL"){

I am using debug mode in VSCODE so when I check:

alteredItem.main = "232"
result.main = "232"

So, I am expecting the result of the above condition to be False but my debug console shows that the value is :

"NULL"

If I remove the ternary operator and change the condition to:

if(alteredItem.main != result.main){

this does return false.

Why does it return "NULL" when I use the ternary operator?

helloitsjoe :

You're mixing an if statement and a ternary expression. You should do either/or, but not both:

Option 1:

if(alteredItem.main != result.main){
  return false;
} else {
  return 'NULL'
}

Option 2:

// It's unclear from your question what you're expecting
// the return value to be, this assumes you want to
// return either `false` or `'NULL'`
return alteredItem.main != result.main ? false : 'NULL';

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=34468&siteId=1