optional chaining
let myMap = new Map();
myMap.set("foo", {name: "baz", desc: "inga"});
let nameBar = myMap.get("bar")?.name;/*
* 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];
}
const array = [1,2,3,4,5];
let arrItem = array?.[4];
console.log(arrItem); /// 5if (user?.team?.subscription?.invoices) {
//
}
Also in JavaScript:
- <br> Parsing error: Unterminated JSX contents
- How to delete an item in an object in redux
- how can we take selected index value of dropdown in javascript
- open pdf in browser javascript
- last item in array javascript
- jquery select plugin
- moment.js
- open close children modal react
- jquery nearest
- how to check if object is empty javascript
- how to push string into array in javascript
- how to create react app using yarn
- fullscreen electron
- javascript wait 10 seconds
- check date clash js
- javascript array add
- drupal 8 node has field
- javasript array indexof
- nestjs AXIOS_INSTANCE_TOKEN
- fizzbuzz js
- jstl spring taglib
- ajax post rest api example
- jqeury cdn
- empty array length javascript