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 Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge

Am I using null incorrectly?

The following is my code for this challenge. Everything works, except that if searchInput is null, it doesn't break the loop. Any insight into what I'm doing wrong is appreciated.

here's my code:

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Vinnie,

Your code works fine for me and breaks the loop if Cancel is pressed however I assume you're referring to what happens when the OK button is pressed without a value? If so you can solve this simply by checking for a falsy value by using the below.

if (!searchInput || searchInput === 'quit') {
  // Code here...
}

What this does is it checks for empty strings, null and even undefined as they all result in a falsy value, one thing to keep in mind is that null and undefined aren't the same as well as empty strings can't be compared to null and undefined.

Hope that helps.

Thanks very much! That clears it up and I've been educated. Much obliged.