Optional chaining was introduced in ES2020 and it can be used with the nullish coalescing operator.
const myNestedProp = user?.profile?.name?.last_name ?? null;
// It will print the value of the nested prop `last_name`
// or null if it's undefined
console.log(myNestedProp);
https://github.com/tc39/proposal-optional-chaining
Answer from parse on Stack OverflowVideos
Optional chaining was introduced in ES2020 and it can be used with the nullish coalescing operator.
const myNestedProp = user?.profile?.name?.last_name ?? null;
// It will print the value of the nested prop `last_name`
// or null if it's undefined
console.log(myNestedProp);
https://github.com/tc39/proposal-optional-chaining
There is no built-in syntax for this. Libraries like lodash have deep-get methods, that solve this problem:
_.get(x, 'deep.path.property')
The above will return the value of x.deep.path.property, or undefined if any of the objects along the way, including x, do not exist.
Such a function is not hard to write. You can do so as a thinking exercise, and get the interface you want (with null instead of undefined), or you can look up the lodash.get implementation (you'll need to follow some imports).
Update
JavaScript now supports the nullish coalescing operator (??). It returns its right-hand-side operand when its left-hand-side operand is null or undefined, and otherwise returns its left-hand-side operand.
Old Answer
Please check compatibility before using it.
The JavaScript equivalent of the C# null coalescing operator (??) is using a logical OR (||):
var whatIWant = someString || "Cookies!";
There are cases (clarified below) that the behaviour won't match that of C#, but this is the general, terse way of assigning default/alternative values in JavaScript.
Clarification
Regardless of the type of the first operand, if casting it to a Boolean results in false, the assignment will use the second operand. Beware of all the cases below:
alert(Boolean(null)); // false
alert(Boolean(undefined)); // false
alert(Boolean(0)); // false
alert(Boolean("")); // false
alert(Boolean("false")); // true -- gotcha! :)
This means:
var whatIWant = null || new ShinyObject(); // is a new shiny object
var whatIWant = undefined || "well defined"; // is "well defined"
var whatIWant = 0 || 42; // is 42
var whatIWant = "" || "a million bucks"; // is "a million bucks"
var whatIWant = "false" || "no way"; // is "false"
function coalesce() {
var len = arguments.length;
for (var i=0; i<len; i++) {
if (arguments[i] !== null && arguments[i] !== undefined) {
return arguments[i];
}
}
return null;
}
var xyz = {};
xyz.val = coalesce(null, undefined, xyz.val, 5);
// xyz.val now contains 5
this solution works like the SQL coalesce function, it accepts any number of arguments, and returns null if none of them have a value. It behaves like the C# ?? operator in the sense that "", false, and 0 are considered NOT NULL and therefore count as actual values. If you come from a .net background, this will be the most natural feeling solution.