📜  es7 - Javascript (1)

📅  最后修改于: 2023-12-03 15:30:38.652000             🧑  作者: Mango

ES7 - JavaScript

ES7 or ECMAScript 2016 is the seventh edition of the ECMAScript language specification. It was finalized in June 2016 and introduced some new features and improvements to the language as well as improving on some of the features introduced in ES6.

In this article, we will be talking about some of the most exciting features that were introduced in ES7.

Includes

The includes method is a new feature in ES7 that enables developers to check whether an array contains a certain value. The method returns a boolean value that indicates whether the given value can be found in the array or not.

const numbers = [10, 20, 30, 40];
console.log(numbers.includes(20)); // true
console.log(numbers.includes(100)); // false
Exponentiation Operator

ES7 introduced the exponentiation operator (**), which allows a much cleaner way to compute the power of a number than using the Math.pow() method.

console.log(2 ** 3); // 8
console.log(3 ** 2); // 9
Array.prototype.flat

Array.prototype.flat is a new method in ES7 that allows developers to flatten arrays recursively.

const arr1 = [1, 2, [3, 4]];
console.log(arr1.flat()); // [1, 2, 3, 4]

const arr2 = [1, 2, [3, [4, 5]]];
console.log(arr2.flat(2)); // [1, 2, 3, 4, 5]
Async Functions

Async Functions is a new feature in ES7 that simplifies asynchronous code execution by using the async and await keywords. It allows developers to write asynchronous code that looks and behaves more like synchronous code.

async function fetchData() {
  const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
  const data = await response.json();
  console.log(data);
}
fetchData();
Object.values & Object.entries

Object.values() and Object.entries() are two new methods introduced in ES7 that allow developers to get the values and key-value pairs of an object respectively.

const user = { name: 'John', age: 30, email: 'john@example.com' };

console.log(Object.values(user)); // [ 'John', 30, 'john@example.com' ]
console.log(Object.entries(user)); // [ [ 'name', 'John' ], [ 'age', 30 ], [ 'email', 'john@example.com' ] ]
Final thoughts

ES7 brought some exciting new features and improvements to the language, making it more powerful and efficient for developers. As a programmer, it's important to keep up with the latest updates and features in the language in order to write better and efficient code.