optional chaining in javascript
const nameLength = db?.user?.name?.length; // property
const adminOption = db?.user?.validateAdminAndGetPrefs?.().option; // functions
const optionLength = db?.user?.preferences?.[optionName].length; // dynamic property
const userName = usersArray?.[userIndex].name; // on arrays/*
* optional chaining (?.) allows me to write code that stops
* running when we encounter a null or undefined value
*/
function tryGetFirstElement<T>(arr?: T[]) {
return arr?.[0];
// equivalent to
// return (arr === null || arr === undefined) ?
// undefined :
// arr[0];
}
let myMap = new Map();
myMap.set("foo", {name: "baz", desc: "inga"});
let nameBar = myMap.get("bar")?.name;if (user?.team?.subscription?.invoices) {
//
}
Also in JavaScript:
- jquery on click return link url
- jquery ajax $.post with data
- install php7 runtime brackets
- node.js crypto.createcipheriv
- react callback
- javascript remove characters from beginning of string
- localStorage
- hark javascript
- angular emit output
- javascript copy array
- best js ide
- cb=gapi.loaded_0:190 Uncaught TypeError: $.ajax is not a function
- hadoop master node storage configuration
- put route error express
- comment in jsp file
- javascript limit input to 2 decimal places
- if statement javascript one line ?
- has class in jquery
- node js store values in file
- tappify.js
- moment.js format
- javascript find similar strings
- how to make a new file javascript
- stop a function javascript