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?
console.log((hello === null ? 'false' : 'true'))
If hello is null, I expected false to come out, but it came out true.
CodePudding user response:
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
.
CodePudding user response:
You have to pass obj.key
obj = {
"hello" : null
}
console.log(obj.hello === null ? false : true)
CodePudding user response:
if(YourObject.hello) {
return true;
}
else{
return false;
}
Using ternary operator
console.log((YourObject.hello) ? true : false);
will evaluate to true if the value is not:
- null
- undefined
- NaN
- empty string ("")
- 0
- false
CodePudding user response:
You are creating an object, but accessing it wrong, you need to access the object's value like this:
obj = {
"hello" : null
}
obj.hello === null ? false : true
CodePudding user response:
if value of hello is null, it returns false anyway, therefore doing that has no meaning.