Is there a standard way to return "Option" (an object that may be null) in Javascript?
For example, is there a more standard way to handle a piece of code like this, especially the function GetById(userId)
?
class User {
static function GetById(userId) {
if (userId === 'A_GOOD_ID') {
return new User('GOOD_NAME');
}
return null;
}
constructor(name) {
this.name = name;
}
}
function authenticate(userId) {
const user = User.GetById(userId);
if (user) return true;
return false;
}