codetogo.io icon indicating copy to clipboard operation
codetogo.io copied to clipboard

Use Case Suggestion: ES6

Open bipon68 opened this issue 1 year ago • 0 comments

// How to add properties to an object using JavaScript Logical And and Spread operators instead of if statement

const salary = null; const pension = 30000; const employee = { id: Math.floor(Math.random() * 10000), name: "Bipon Biswas", ...(salary && {salary}), ...(pension && {pension}) } console.log(employee) Output: {id: 4163, name: 'Bipon Biswas', pension: 30000}

bipon68 avatar Jul 19 '22 19:07 bipon68