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 Basics (Retired) Creating Reusable Code with Functions Getting Information From a Function

return a functions

can i not write

if (isEmailEmpty() ===true { alert("give email"); }

have i to create a variable for this ??

While you can do it this way, it is considerably harder to read than:

var result;
result = isEmailEmpty();

if (result === true)
{
   alert("give email");
}

So yes, it can be done but unless it's a small task and you're the only one who will ever have to read the code, it may pay dividends to be a little longwinded. I've always understood explicit coding to be considered best practice, granted I am getting back into programming after several years away from the field so maybe opinions have changed.

Boban Talevski
Boban Talevski
24,793 Points

Late to the party, but taking this course now, so here's my take coming from the Java world.

if (isEmailEmpty()) { // guess there's no need to compare it to true
  alert('give email');
}

is pretty legible and understandable considering the function name is very descriptive and it looks more understandable to me than having a separate variable to store the function result. And it reads pretty well in plain English, like if the email is empty show the alert to enter email.

Otherwise, what would be a good name for the variable? If I had to, I would probably name it isEmailEmpty :) , so doesn't really add much to legibility, only to the confusion. You have a function and a boolean variable with the same name now and need to distinguish between them.

I assume that for teaching purposes it is probably better to have it first stored in a boolean variable though, but I don't think it's a best practice down the road.

Just my 2 cents, coming from the Java world. I would never add a variable in this case to hold the result of a function with such a descriptive name.

1 Answer

Mohammed Khalil Ait Brahim
Mohammed Khalil Ait Brahim
9,539 Points

You can If your function returns something ! Here is a small example. What you did is fine though I see that you need to close the parenthesis after the condition :

var test = function() {
  return true;
}

if(test() === true) {
 // Do Something
}