Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created April 8, 2023 19:39
Show Gist options
  • Save prof3ssorSt3v3/3353db8c2f16f7124a548f91c538ef4a to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/3353db8c2f16f7124a548f91c538ef4a to your computer and use it in GitHub Desktop.
code from video about nullish coalescing assignment operator
//nullish coalescing assignment ??=
//related to the nullish coalescing operator ??
//overwrite IF nullish (null or undefined)
import crypto from 'crypto';
//import not required in browser
const u1 = {
name: 'brand new user',
email: '[email protected]',
};
const u2 = {
id: '1be4abee-7e9c-4d4a-9084-fa1390291009',
name: 'Bono',
email: '[email protected]',
};
function saveAUser(user) {
//function to edit or create a user
// user.id = user.id ?? crypto.randomUUID();
user.id ??= crypto.randomUUID();
//if user.id does not currently have a value
console.log(user);
}
saveAUser(u1);
saveAUser(u2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment