ES2020 Features

String#matchAll

				
					let text = `India is the world's largest producer of milk, pulses and Jute.
India has 43 UNESCO World Heritage Sites.
India has many languages, including 22 officially
recognized languages and thousands of dialects.
I love my india
`
const iterator = text.toLowerCase().matchAll("india");
var result =[...iterator]
console.log(result.toString());
console.log(result.map(x=>x.index));

india,india,india,india
[ 0, 64, 106, 214 ]
				
			

Promise.allSettled

				
					const p1 = new Promise((resolve, reject) => {
  reject("p1 failed");
});

const p2 = new Promise((resolve, reject) => {
  setTimeout(resolve, 6000, "p2 done");
});

const p3 = new Promise((resolve, reject) => {
  setTimeout(resolve, 15000, "p3 done");
});

const p4 = new Promise((resolve, reject) => {
  setTimeout(reject, 5000, "p4 rejected");
});

Promise.allSettled([p1, p2, p3, p4]).then((value) => {
  console.log(value);
 
});


/*output
The output prints after 15 seconds
[
  { status: 'rejected', reason: 'p1 failed' },
  { status: 'fulfilled', value: 'p2 done' },
  { status: 'fulfilled', value: 'p3 done' },
  { status: 'rejected', reason: 'p4 rejected' }
]

*/
				
			

Optional Chaining

				
					const empobj = {
  name: 'Anurag',
  Reportees: {
    name: 'Anil',
  },
  Department :{},
};

const location = empobj.Location?.name;
console.log(location);
//undefined
				
			

Nullish Coalescing

				
					let name = null;
console.log(name ?? "default name")

				
			

??=

				
					let x;
x ??= 10;

OUTPUT:- 10

/*If the first value is undefined or null, the second value is assigned.*/
				
			

||=

				
					let x = 100;
x ||= 15;

OUTPUT :- 100

/*If the first value is false, the second value is assigned.*/
				
			

&&=

				
					let x = 100;
x &&= 55;

OUTPUT:- 55
/*If the first value is true, the second value is assigned.*/
				
			

Leave a Comment