DEV Community

Discussion on: Some Best Practices of Javascript for clean and better code quality...

Collapse
 
fhefh2015 profile image
fhefh2015 • Edited

Logical assignment

There might be hte scenario where we need to assign something when any given variable is null or undefined, normal way using if would be like this:

Logical assignment

let userProfile;
let value = {
   name: 'someValue',
};
if(userProfile === null || userProfile === undefined) {
   userProfile = value
}

 //   OR
if(!userProfile) {
   userProfile = value
}


userProfile ??= value;

// OR

userProfile &&= value
Enter fullscreen mode Exit fullscreen mode

It's much easier to write, but the readability is so bad.