The spread and rest operators in JavaScript are two powerful features introduced in ES6 that allow developers to work with arrays and objects more conveniently.
The spread operator is represented by three dots (i.e., ...
) and allows an iterable (such as an array or string) to be expanded in places where zero or more arguments or elements are expected. It is commonly used for:
The rest operator is also represented by three dots (i.e., ...
) and is used to gather a variable number of arguments into an array. It is often used in function definitions when you do not know how many arguments will be passed. It helps create more flexible functions.
// Spread operator example
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4, 5, 6]
// Rest operator example
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
console.log(sum(1, 2, 3, 4)); // 10
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?