1

In JS's optional chaining (I don't think it's implemented in current versions), can I have it evaluate to false if not found?

var obj = {a: {foo: "bar"}}
console.log(obj.a?.foo); //bar
console.log(obj.b?.foo); //this is undefined, I want it to be false

I read Optional Chaining in JavaScript and Null-safe property access (and conditional assignment) in ES6/2015 but neither answered my question.

i'm a girl
  • 178
  • 1
  • 10

1 Answers1

0

You can use !! or != null (pay attention != not !== to check both undefined and null)

var obj = {a: {foo: "bar"}};
console.log(!!obj.b?.foo);
console.log(obj.b?.foo != null);
Reza
  • 15,335
  • 4
  • 62
  • 120