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 trial

JavaScript JavaScript Functions Create Reusable Code with Functions Using Multiple return Statements

Why we don't simply return !field.value?

Instead of conditional statement I'd simply return !field.value. Is this solution was provided because of readability issue? There are few ways to solve it then. Let's say to declare const fieldIsEmpty = !field.value; return fieldIsEmpty. Am I missing something?

2 Answers

Steven Parker
Steven Parker
231,236 Points

You can optimize it even further by omitting the variable entirely:

    return !field.value;

The sample optimizations shown in the video aren't intended to be the only ones possible. The fact that you're spotting even more compact/efficient solutions is a good indication of your learning progress and grasp of the material! :+1:

That would have side effects.

any "falsey" value would be considered empty. For example the value 0 would be falsey, and therefore considered empty.

Steven Parker
Steven Parker
231,236 Points

Since "value" is a string, it can't have the value 0. Only an empty string would appear "falsey".

as long as you can be sure that the value indeed always will be a string