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": null


 "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?

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.

Leave a Reply