What is the most efficient way to copy some properties from an object in JavaScript?

You can achieve it with a form of destructuring:

const user = { _id: 1234, firstName: 'John', lastName: 'Smith' };
const { _id, ...newUser } = user;

Browser support:

The spread (...) syntax was introduced with ES2018, and most browsers even supported it before ES2018 was finalized. So as of 2023, you can rely on browser support unless you need to support very old browsers.
Alternatively, you may use it with a “transpilation” layer such as Babel.

Leave a Comment