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

terry okey
terry okey
3,187 Points

I'm not sure about the exclamation point operator

In this code:

if (!field.value) { return true; } else{ return false; }

Does the exclamation point in front of a item always check for true or false? How does it equate an empty field as false? Or true, wait. The exclamation point in front always means it's looking for the opposite, yes? In this case the opposite would be the field being empty. so it returns true. What would it return if the exclamation point was not there?

1 Answer

Robert Manolis
STAFF
Robert Manolis
Treehouse Guest Teacher

Hey terry okey , the exclamation point operator is called the "Logical Not" operator. You can think of it as looking for the opposite of what would be looked for if it weren't there. In your example, if (!field.value) { return true; } else{ return false; }, the condition being evaluated is this: check if the field.value is not true. And if it is not true, then return true, else return false. So without the not operator there, the condition would be checking if the field.value is true, and then the conditional would return true if it the condition is true, and false if not.

Hope that helps. :smiley:

terry okey
terry okey
3,187 Points

Haha, "Logical not", got it, thanks. That's roughly what I thought, I can see where that would be useful, but boy does that get confusing. But also is it always the case that if you test for a string, if the string is empty it comes back false? So obviously you would be testing for a string with the boolean, in this case "if", and if there are no letters in the string it comes back false?