• Home
  • 8 Exciting New JavaScript Concepts You Need to Know

As a developer, staying up-to-date with the latest advancements in JavaScript is crucial to writing efficient, modern, and scalable code. In this post, we’ll explore 8 new and exciting JavaScript concepts that you should know to take your coding skills to the next level.

1. Optional Chaining (?.)

Introduced in ECMAScript 2020, optional chaining allows you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

let name = person?.address?.street?.name;

2. Nullish Coalescing (??)

Also introduced in ECMAScript 2020, the nullish coalescing operator returns the first operand if it’s not null or undefined, and the second operand otherwise.

let name = person?.name?? 'Unknown';

3. BigInt

A new numeric primitive in JavaScript, BigInt is used to represent integers with arbitrary precision, allowing for accurate calculations with large integers.

const x = 12345678901234567890n;

4. globalThis

A new global object, globalThis, provides a way to access the global object in a way that’s compatible with modern JavaScript environments.

console.log(globalThis === window); // true in a browser

5. matchAll()

A new method on the String prototype, matchAll() returns an iterator that yields matches of a regular expression against a string, including capturing groups.

const regex = /(\w)(\d)/g;
const str = 'a1b2c3';
for (const match of str.matchAll(regex)) {
  console.log(match);
}

6. Promise.allSettled()

A new method on the Promise API, allSettled() returns a promise that is resolved when all of the promises in an array are either resolved or rejected.

const promises = [Promise.resolve('a'), Promise.reject('b'), Promise.resolve('c')];
Promise.allSettled(promises).then((results) => console.log(results));

7. String.prototype.at()

A new method on the String prototype, at() returns the character at the specified index, allowing for negative indices to access characters from the end of the string.

const str = 'hello';
console.log(str.at(0)); // 'h'
console.log(str.at(-1)); // 'o'

8. Error Cause

A new property on Error objects, cause allows you to specify the underlying cause of an error.

try {
  throw new Error('Error occurred', { cause: new Error('Underlying cause') });
} catch (error) {
  console.log(error.cause);
}

Author: Muhammad Talha Waseem

Leave Comment