Created
April 8, 2023 19:39
-
-
Save prof3ssorSt3v3/3353db8c2f16f7124a548f91c538ef4a to your computer and use it in GitHub Desktop.
code from video about nullish coalescing assignment operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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