DEV Community

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

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

but the best way and the recommended way is using spread operator

No it's not. It simply does a different thing, but it's not "better" or "worse" than setting the key directly, and claiming so is just very misleading.

Collapse
 
capscode profile image
capscode

I said in Production application... its useful.
When the same scenario comes in React JS its pretty much helpful and easy if you use spread operator

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

That was not your claim though; of course spreading into a new object has many uses, but saying it is the "best" way is plainly wrong. It's not. In fact, in many cases, it is objectively worse than the alternative of directly setting the key.