Sometimes, we want to check for null inline in JavaScript.
In this article, we’ll look at how to check for null inline in JavaScript.
How to check for null inline in JavaScript?
To check for null inline in JavaScript, we use the optional chaining operator.
For instance, we write
const example = { a: ["first", { b: 3 }, false] };
console.log(example?.a);
console.log(example?.b);
console.log(example?.a?.[0]);
console.log(example?.a?.[1]?.a);
console.log(example?.a?.[1]?.b);
to use the optional chaining ?.
operator to return undefined
if any property in the chain is null
or undefined
.
Conclusion
To check for null inline in JavaScript, we use the optional chaining operator.