codetogo.io
codetogo.io copied to clipboard
Use Case Suggestion: ES6
// 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}