DEV Community

Discussion on: Set Variable As A Key Name In JavaScript Object

Collapse
 
ksengine profile image
Kavindu Santhusa

When you want to get a shallow copy of object. Use {...obj1} syntax. If you want to copy 2 objects to 1. Use {...obj1, ...obj2} method.

Example

let defaultOptions = { a: 1, b: 2}
function myFuntion(input, options={}) {
    let newOptions = {...defaultOptions, ...options}
    ...
}
Enter fullscreen mode Exit fullscreen mode