Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBen McMahan
7,922 PointsShorthand solution?
I known that we haven't really covered shorthand if...else, but how about something like this?
/**
* Checks if space has an associated token to find its owner
* @return {(null|Object)} Returns null or the owner object of the space's associated token.
*/
get owner() {
return this.token ? this.token.owner : null;
}
1 Answer
Steven Parker
231,248 PointsWhile that would probably work, counting on this.token to only appear "falsey" when null is a bit obscure. This would be a more direct translation of the original solution using the ternary operator:
get owner() {
return (this.token === null) ? null : this.token.owner;
}
You could also do an even more terse shorthand using the optional chaining and nullish coalescing operators:
get owner() {
return this.token?.owner ?? null;
}