Optional chaining allows to safely access nested properties or methods of an object without having to check for the existence of each intermediate
property manually. It provides a concise and safe way to access nested properties or methods without having to write complex and error-prone
null/undefined checks.
This rule flags logical operations that can be safely replaced with the ?. optional chaining operator.
Replace with ?. optional chaining the logical expression that checks for null/undefined before accessing the
property of an object, the element of an array, or calling a function.
function foo(obj, arr, fn) {
if (obj && obj.value) {}
if (arr && arr[0]) {}
if (fn && fn(42)) {}
}
function foo(obj, arr, fn) {
if (obj?.value) {}
if (arr?.[0]) {}
if (fn?.(42)) {}
}