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) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

Carlos R Q
Carlos R Q
4,411 Points

if (isAdmin) ? Why is there no thing to compare?

Hi everyone! Can someone explain me the diference of using just "if (isAdmin)" instead of adding the triple equal? I assume that the browser understands that the variable must be compare to a true value, but where is the comparison or if it is not a true value: what does it compare it to? Thanks!

var isAdmin = false;
var isStudent = true;

if ( isAdmin ) {
    alert('Welcome administrator')
} else if ( isStudent) {
  alert('Welcome Student');
}

When you are using just, if(isAdmin) , you are basically saying if(isAdmin == true) { perform action}

Carlos R Q
Carlos R Q
4,411 Points

Yeah, now i get it , thanks for the reply!

4 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Carlos,

With if (isAdmin) the conditional is just checking the Boolean value of the variable named 'isAdmin.' So, if isAdmin is set to TRUE, the condition passes and the code is executed. If the isAdmin variable to set to FALSE, the condition fails and it moves on to the next conditional.

The reason you don't need a == or === is because you are only checking against 2 possible returns -- "True" or "False," as only a Boolean can be returned with this syntax.

Anytime you are using a conditional statement, you are looking for a TRUE or FALSE return, so, you would use == or === when there is multiple possibles to check against. For example, if you are checking an input from a user against a predetermined answer.

I hope this makes sense and clears it up for you. Keep Coding! :)

Steef Vendy
Steef Vendy
9,436 Points

Hi Jason,

I am having issue with the challenge: the question is Add an else if clause that tests to see if the isStudent variable is true. If it is then open an alert dialog with the message 'Welcome student'.

and here are my codes:

var isAdmin = false;
var isStudent = true;

if ( isAdmin ) {
    alert('Welcome administrator')
} else if (isStudent) {
  alert('Welcome Student');
}

and I am having this bummer! Hmm. I don't see the message 'Welcome student' in the alert message.

I noticed it works on browser tho, what is wrong? help.

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

HI Steef Vendy,

Your code is correct. However, the challenges are very picky and very strict with what it wants... With that in mind, it is just a small typo that is giving you the "Bummer." The challenge wants an alert box with "Welcome student" where "student" has a lower-case "s". Just change your capital "s" and you're good to go.

:)

Presley Cobb
Presley Cobb
9,214 Points

Conditional if statements check for a true or false value before it runs the code. So if you place a variable that evaluates to true the code will run and vis versa.

Carlos R Q
Carlos R Q
4,411 Points

Thanks for the help , now I have it more clear what does it mean, I have some other doubts but I'll check them first before I ask! Thanks again!

Don't forget the semicolon(s) in your conditional code blocks. You forgot the semicolon after alert('Welcome administrator') line. Try that fix also.

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Hi Carolyn,

While (some coders say) semicolons in JavaScript are mostly a good idea, they are actually not required, especially in this example. You could have semicolons after both the alert statements or after neither, and both would be correct and parse properly. In JavaScript the semicolon is not a statement terminator but a statement separator. This is also accomplished with a line break (an argument Against the use). This, however, could cause problems if you minify the JS file (an argument For the use).

Like many things, there is always debate about this. Some people say to always use them, while others say not to use them. See the discussion here on StackOverflow on should they be used or this discussion on Codecademy on when 'NOT' to use them.

:)