Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javscript Ternary operator if json value is null

I want to return false if null is returned, but it keeps returning true even when it returns null.

hello.json.

{
  "hello": null
}

or 

{
 "hello" : {
    id: 1,
    hi: 'hi'
   } 
}
console.log(hello === null ? false : true) 

The hello value is output as a null value and I want to return false. What should I do?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

console.log((hello === null ? 'false' : 'true'))

If hello is null, I expected false to come out, but it came out true.

>Solution :

Add console.log(hello); before your current console.log to find out what is actually coming into the hello variable/field.

I think the current value of hello is undefined, so the result in your conditional statement is true.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading