ES13 (ECMAScript 2022) introduced top-level await, class fields, Array.prototype.at, and Object.hasOwn method.
June 2022
ES13 (ECMAScript 2022) introduced top-level await, class fields, Array.prototype.at, and Object.hasOwn method.
Try out JavaScript code examples and experiment with features in the official TypeScript playground.
Open JavaScript PlaygroundUse await at the top level of modules without wrapping in an async function.
// In a module file
const response = await fetch("https://api.example.com/data");
const data = await response.json();
export { data };
// Can be used in other modules
import { data } from "./data.js";
console.log(data);Declare class fields directly in the class body without constructor.
class Person {
// Public fields
name = "John";
age = 30;
// Private fields
#secret = "private data";
constructor(name, age) {
this.name = name;
this.age = age;
}
getSecret() {
return this.#secret;
}
}
const person = new Person("Jane", 25);
console.log(person.name); // "Jane"
console.log(person.getSecret()); // "private data"Access array elements using negative indices (counting from the end).
const arr = [1, 2, 3, 4, 5];
console.log(arr.at(0)); // 1 (first element)
console.log(arr.at(-1)); // 5 (last element)
console.log(arr.at(-2)); // 4 (second to last)
console.log(arr.at(10)); // undefined (out of bounds)
// Useful for getting last element
const last = arr.at(-1); // Better than arr[arr.length - 1]Safe way to check if an object has a property as its own property.
const obj = { a: 1 };
console.log(Object.hasOwn(obj, "a")); // true
console.log(Object.hasOwn(obj, "b")); // false
console.log(Object.hasOwn(obj, "toString")); // false (inherited)
// Safer than hasOwnProperty
const obj2 = Object.create(null);
obj2.a = 1;
// This would throw an error:
// obj2.hasOwnProperty("a");
// This works:
console.log(Object.hasOwn(obj2, "a")); // true