Welcome to CodeYourCraft's ES11 Features Tutorial! In this lesson, we'll explore exciting new JavaScript features introduced in ECMAScript 2019 (ES11). By the end, you'll have a solid understanding of these modern enhancements, perfect for both beginners and intermediates.
Let's dive right in! ๐ณ
ES11 stands for ECMAScript 2019, the latest version of the JavaScript standard. It includes several new features that make coding easier, more efficient, and more enjoyable.
Before ES11, arrow functions required explicit return statements. Now, we can use fat arrow functions to return an object directly.
// Old style
const obj = {
prop: function() {
return { result: 'Success' };
}
}
// New style (ES11)
const obj = {
prop: () => ({ result: 'Success' })
}Arrow functions no longer bind their 'this' to the parent context. Instead, they inherit 'this' from the enclosing scope.
const car = {
brand: 'Tesla',
getBrand() {
return this.brand;
},
driver = {
name: 'John',
displayDetails() {
console.log(this.name, this.getBrand());
}
}
}
car.driver.displayDetails(); // "John Tesla"The nullish coalescing operator (??) safely checks if a value is either null or undefined. If so, it returns the right-hand operand.
const name = null;
const defaultName = 'John Doe';
const fullName = name ?? defaultName;
console.log(fullName); // "John Doe"Array.flat() flattens nested arrays to a single level, and Array.flatMap() combines flattening and mapping into one function.
const nestedArray = [1, 2, [3, 4, [5, 6]], 7];
const flatArray = nestedArray.flat(2); // Flattens up to 2 levels
console.log(flatArray); // [1, 2, 3, 4, [5, 6], 7]
const mappedArray = nestedArray.flatMap(item => Array.from(item));
console.log(mappedArray); // [1, 2, 3, 4, 5, 6, 7]Now you can use logical operators directly in template literals.
const a = 5;
const b = 10;
console.log(`The result of ${a > b ? 'a' : 'b'} is greater.`); // "The result of b is greater."Tagged templates allow you to create custom template behavior.
function taggedTemplate(string, ...values) {
console.log('Running a custom template...');
// Custom logic here
// Return the final output
}
taggedTemplate`Hello, ${name}!` // "Running a custom template..."
// Output will be custom depending on your logicThese methods remove whitespace from the start or end of a string.
const str = ' Hello, World! ';
const trimmedStr = str.trimStart();
const trimmedStrEnd = str.trimEnd();
console.log(trimmedStr); // " Hello, World!"
console.log(trimmedStrEnd); // " Hello, World"Sticky regular expressions match from the current position, not the start, reducing backtracking.
const str = 'aaaabbbbccc';
const regex = /(a+)/y;
const matches = str.match(regex);
console.log(matches); // ["aaa", index: 0, input: "aaaabbbbccc"]In ES11, the catch() and finally() methods can handle nullable Promise values.
const promise = new Promise((resolve, reject) => {
// Promise implementation
});
// Handle nullable values in the catch() block
promise.catch(error => {
console.log(error);
});
// The finally() block runs regardless of the Promise result
promise.finally(() => {
console.log('Finally block executed.');
});The rest parameter can be used with array destructuring to assign the remaining elements to a variable.
const numbers = [1, 2, 3, 4, 5];
const [first, second, ...remaining] = numbers;
console.log(first); // 1
console.log(second); // 2
console.log(remaining); // [3, 4, 5]The spread operator can be used to copy properties from one object to another when destructuring.
const person = {
name: 'John',
age: 30
};
const { name, ...other } = person;
console.log(name); // "John"
console.log(other); // { age: 30 }That wraps up our introduction to ES11 features! These improvements help make JavaScript more efficient, expressive, and enjoyable. Try experimenting with these features in your own projects, and remember to come back to CodeYourCraft for more in-depth tutorials on each topic. ๐
What does the nullish coalescing operator (??) do?
By following this tutorial, you've learned about ES11 features, including arrow function updates, object and array improvements, template literals enhancements, strings and regular expressions updates, Promises enhancements, and destructuring assignment updates. With this newfound knowledge, you'll be well on your way to mastering the latest additions to JavaScript. Happy coding! ๐ฏ ๐