Exploring the Latest ECMAScript Features: What’s New in JavaScript

Pawan Kumar
3 min readDec 17, 2023

--

Introduction:

JavaScript, as the language of the web, is continually evolving to meet the demands of modern development. The ECMAScript specification, which serves as the foundation for JavaScript, regularly introduces new features and enhancements. In this exploration, we’ll delve into some of the latest ECMAScript features that have been added to JavaScript, showcasing how they can elevate your coding experience.

**1. ECMAScript Modules (ESM):

Introduction: ECMAScript Modules introduce a standardized module system for JavaScript, allowing developers to structure code into reusable and maintainable components.

Usage:

// Importing module
import { functionA, variableB } from './module';

// Exporting module
export function functionC() {
// Module content
}

**2. Nullish Coalescing Operator (??):

Introduction: The Nullish Coalescing Operator provides a concise way to handle default values for variables that may be null or undefined.

Usage:

const value = null;
const defaultValue = 'Default Value';

const result = value ?? defaultValue;
console.log(result); // Output: 'Default Value'

**3. Optional Chaining (?.):

Introduction: Optional Chaining simplifies the process of accessing nested properties in objects, allowing developers to avoid lengthy conditional checks.

Usage:

const user = {
profile: {
address: {
city: 'Example City',
},
},
};

const city = user?.profile?.address?.city;
console.log(city); // Output: 'Example City'

**4. BigInt:

Introduction: BigInt is a new primitive type in JavaScript that enables the representation of arbitrarily large integers beyond the limits of the Number type.

Usage:

const bigNumber = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);
console.log(bigNumber); // Output: 9007199254740992n

**5. Promise.allSettled:

Introduction: Promise.allSettled returns a promise that resolves after all the given promises have either resolved or rejected, providing information about each promise’s outcome.

Usage:

const promises = [Promise.resolve('Resolved'), Promise.reject('Rejected')];

Promise.allSettled(promises)
.then(results => console.log(results))
.catch(error => console.error(error));

**6. String.prototype.matchAll:

Introduction: String.prototype.matchAll returns an iterator of all regular expression matches in a string, including capturing groups.

Usage:

const regex = /(\w+) (\w+)/g;
const text = 'John Doe, Jane Smith';

const matches = [...text.matchAll(regex)];

console.log(matches);
// Output: [
// ['John Doe', 'John', 'Doe'],
// ['Jane Smith', 'Jane', 'Smith']
// ]

**7. GlobalThis:

Introduction: GlobalThis provides a standard way to access the global object, regardless of the environment (e.g., browser or Node.js).

Usage:

console.log(GlobalThis === window); // In a browser environment
console.log(GlobalThis === global); // In a Node.js environment

**8. Logical Assignment Operators (||=, &&=, ??=):

Introduction: Logical Assignment Operators provide concise syntax for assigning a variable only if it is currently falsy (for ||= and &&=) or null or undefined (for ??=).

Usage:

let variable = 'Existing Value';

variable ||= 'New Value';
console.log(variable); // Output: 'Existing Value'

let nullableVariable = null;

nullableVariable ??= 'Default Value';
console.log(nullableVariable); // Output: 'Default Value'

Conclusion:

These are just a few highlights from the latest ECMAScript features in JavaScript. Staying informed about these advancements empowers developers to write more concise, expressive, and efficient code. As you explore and integrate these features into your projects, you’ll discover new possibilities for enhancing your JavaScript development experience.

--

--

No responses yet