Skip to content

Instantly share code, notes, and snippets.

@alperg
Last active October 17, 2019 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alperg/66886b52a72098831f89186ed782529d to your computer and use it in GitHub Desktop.
Save alperg/66886b52a72098831f89186ed782529d to your computer and use it in GitHub Desktop.
Add conditional props
// In this example, password will be added only when password is truthy!
const user = { id: 100, name: 'Howard Moon' };
const password = 'Password!';
const userWithPassword = {
...user,
id: 100,
...(password && { password })
};
console.log(userWithPassword); // { id: 100, name: 'Howard Moon', password: 'Password!' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment