Lesser‑Known but Useful ES6 Features and Techniques
This article introduces several relatively obscure yet practical ES6 features—including Object.entries/Object.fromEntries, Symbol, WeakMap/WeakSet, Promise.allSettled, BigInt, Array.of/Array.from, and the .at and flat methods—explaining their purpose and providing code examples for each.
Introduction
ES6 (ECMAScript 2015) introduced many new features and syntax; some of them are relatively obscure but highly practical. This article presents a collection of such advanced techniques.
Object.entries()
Object.fromEntries()
Symbol type and Symbol properties
WeakMap and WeakSet
Promise.allSettled()
BigInt
Array.of
Array.from
.at and flat
1. Object.entries() and Object.fromEntries()
Object.entries() returns an array of a given object's own enumerable property [key, value] pairs.
Object.fromEntries() converts a list of key‑value pairs back into an object.
Example of Object.entries():
const obj = { a: 1, b: 2, c: 3 };
const entries = Object.entries(obj);
console.log(entries); // [["a", 1], ["b", 2], ["c", 3]]Example of Object.fromEntries():
const entries = [["a", 1], ["b", 2], ["c", 3]];
const obj = Object.fromEntries(entries);
console.log(obj); // { a: 1, b: 2, c: 3 }2. Symbol Type and Symbol Properties
Symbol is a new primitive type used to create unique identifiers. Symbol‑keyed properties can be added to objects without risking name collisions.
const sym = Symbol('description');
const obj = {
[sym]: 'value'
};
console.log(obj[sym]); // value3. WeakMap and WeakSet
WeakMap is a collection where keys must be objects and are garbage‑collected when there are no other references. WeakSet works similarly for values.
const wm = new WeakMap();
const obj = {};
wm.set(obj, 'value');
console.log(wm.get(obj)); // value
const ws = new WeakSet();
ws.add(obj);
console.log(ws.has(obj)); // true4. Promise.allSettled()
Promise.allSettled() returns a promise that resolves after all given promises have settled (either fulfilled or rejected), providing an array of result objects.
const promises = [
Promise.resolve('resolved'),
Promise.reject('rejected'),
Promise.resolve('resolved')
];
Promise.allSettled(promises)
.then(results => console.log(results))
.catch(error => console.error(error));
// Output:
// [
// { status: 'fulfilled', value: 'resolved' },
// { status: 'rejected', reason: 'rejected' },
// { status: 'fulfilled', value: 'resolved' }
// ]5. BigInt
BigInt is a new primitive type for representing integers of arbitrary precision.
const bigIntValue = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);
console.log(bigIntValue); // 9007199254740992n6. Array.of and Array.from
Array.of() creates a new array from a variable number of arguments.
Array.from() creates a new array from an array‑like or iterable object, optionally applying a mapping function.
const arr1 = Array.of(1, 2, 3);
console.log(arr1); // [1, 2, 3]
const str = 'Hello';
const arr = Array.from(str);
console.log(arr); // ['H', 'e', 'l', 'l', 'o']
const nums = [1, 2, 3, 4, 5];
const doubled = Array.from(nums, num => num * 2);
console.log(doubled); // [2, 4, 6, 8, 10]7. .at and flat
.at() returns the element at a given index, supporting negative indices.
flat() flattens nested arrays into a new array, with an optional depth argument.
const arr3 = [1, 2, 3, 4, 5];
console.log(arr3.at(2)); // 3
const arr4 = [1, [2, [3]]];
console.log(arr4.flat()); // [1, 2, [3]]Conclusion
ES6 introduced many practical yet relatively lesser‑known advanced techniques. Object.entries() and Object.fromEntries() simplify conversion between objects and key‑value pairs. Symbol provides unique identifiers. WeakMap and WeakSet offer automatically garbage‑collected collections. Promise.allSettled() handles multiple promises and returns all results. BigInt enables arbitrary‑precision integers. Array.of , Array.from , .at , and flat give more convenient array operations, helping developers write more efficient code.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.