js Intermediate
ECMAScript new features
Explore ECMAScript
const data = {};
const person = {
name: 'Adrian',
age: 27
};
const animals = ['Dog', 'Cat'];
const nestedAnimals = [['Dog'], ['Cat'], ['Horse']];
// Thanks to the "Elvis operator" or more known as optional chaining,
// we can safely access nested object properties even if they don't exist
console.log(data?.a?.b?.c); // undefined instead of error
// We can do the similar with arrays, since arrays are indexed from 0,
// animals[2] is the 3rd element which doesn't exist
console.log(animals[2]?.toString()); // undefined instead of error
// We can also use optional chaining on functions, in this case
// we are trying to call "myImaginaryFunction" - which again, doesn't exist
console.log(data.myImaginaryFunction?.()); // undefined instead of error
// We can still use optional chaining to access nested properties
console.log(person?.name); // "Adrian"
// Null coalescing operator is best suited for checking against null or undefined
// It will accept other "falsy" values such as 0
const zero = 0 ?? 5;
const five = null ?? 5;
const ten = undefined ?? 10;
console.log(zero, five, ten); // 0, 5, 10
// .flat creates a flatten array, it accepts depth as an optional
// parameter of how many levels it should flatten the original array
// by default it's 1 level
const flattenAnimals = nestedAnimals.flat();
console.log(flattenAnimals); // ["Dog", "Cat", "Horse"]
Summary
- ECMAScript brings up new features every year to the JavaScript world.
- It’s challenging to stay up-to-date but also rewarding.
- Optional chaining is a true game-changer for writing code.
- If it comes to arrays, worth taking a look at all the available methods e.g. on devdocs.io.
Try this in our interactive code editor
Practice hands-on with our built-in code sandbox.
Open Code Editor
Adrian Bigaj
Creator of BigDevSoon
Full-stack developer and educator passionate about helping developers build real-world skills through hands-on projects. Creator of BigDevSoon, a vibe coding platform with 21 projects, 100 coding challenges, 40+ practice problems, and Merlin AI.
Related Pills
js Intermediate
JavaScript multiple condition checks
Checking multiple values in JavaScript often leads to redundant, boilerplate code - let's use .includes method to fix that.
js Intermediate
Convert JavaScript data structures
Converting data structures is a day-to-day job as a Developer.
js Intermediate
Remove duplicates in JavaScript
Clones might take over the world, we need to learn how to remove duplicates from an array.