Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript optional add object key

const gender = 'male'
const data = {
  first_name: 'Joe',
  last_name: 'Doe',
  ...(gender && {gender})
}
console.log(data)
// {
//   first_name: 'Joe',
//   last_name: 'Doe'
//   gender: 'male'
// }
 
PREVIOUS NEXT
Tagged: #javascript #optional #add #object #key
ADD COMMENT
Topic
Name
3+6 =