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
  • 308
  • 3
  • 11

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
  • 18,865
  • 13
  • 88
  • 163