Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Displaying the Search Results

Dennis Brown
Dennis Brown
28,742 Points

Why the long conditional?

I'm curious why you would want to look for "> 0", instead of just using the built-in value conditional from the value itself?

Example:

...
if(results.length) {
  gifs = results.map(gif => {
...

I only ask this, because this is probably one of the most common conditional patterns I've seen used in React; not only for arrays, but also for boolean switches. It just looks and feels much cleaner, as well as much more dry. I'm sure it's mostly a styling choice, but I cannot think of a reason why it would be a logical choice since 0/undefined will always return false.

I guess I could say the same about the ternary parens in this case, but I know that is a styling choice.

Anyways, I thought I would at least bring it up.

2 Answers

Most likely it is being specified for verbosity and learning. Shorthand the if clause to results.length is more 'advanced' to some. Should be safe to leave it as you have written it.

I would like to point out though that 0 (zero) and undefined will indeed be false, but in javascript -1 is not a falsy value. So be careful with that idea.

Hi Dennis,

Joe brings up an important point here for the general case.

In the general case, checking if a value is greater than 0 is not the same as checking if a value is truthy.

It's not a concern here because the length property can't be negative. But in general, it would be a concern.

Dennis Brown
Dennis Brown
28,742 Points

Ah, great point!

Yes, for some reason I thought -1 was falsy, but ran another test to show it does return true. Of course I cannot think of a time an array would do this, and would need to use a method like indexOf for that to be an issue. The main concern though is the array, and testing for !0, and undefined if something was to happen with the state. It seems too redundant, and something I constantly see being used not only by myself. I understand making it explicit "just in case", but in a case like this there really isn't a reason to worry.

EDIT: Thanks for making me recheck that. I know there are a few conditionals that offer results not expected, and anything that watches for -1 (e.g. indexOf), I will use a conditional operator, otherwise I simply do not run across it. Still, it's great to remember, and I thank you for that.

If you don't have any images, that will be falsy.

Dennis Brown
Dennis Brown
28,742 Points

That's the point. It's redundant. Instead you can use result.length as the conditional, as the return value of "0" will return false.