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 A Basic If Statement

Tyler Darnell
Tyler Darnell
1,886 Points

JavaScript Basics: Code Challenge: A Basic If Statement Write a conditional statement where the var is true with alert.

Add a conditional statement that test to see if the isAdmin variable is true. If it is then open an alert with the message "Welcome administrator".

I'm sure this is really silly. I haven't coded in a few days because I haven't had reliable internet. Last time I got stuck it was because I capitalized the 'If' in my conditional statement, and I learned from that mistake. I appreciate any help.

Thank you in advance.

script.js
var isAdmin = true;
var isStudent = false;
if (var isAdmin = true) {
  alert('<p>Welcome administrator</p>');
}
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

5 Answers

Stanley Thijssen
Stanley Thijssen
22,831 Points

Hi Tyler,

You shouldnt use the var inside your if statement. Just use isAdmin instead.

You have the right idea but just a couple things need to change in your conditional.

var isAdmin = true;
var isStudent = false;
if (isAdmin) {
  alert('<p>Welcome administrator</p>');
}

You have already declared isAdmin and do not require to declare it again. Also, the = true is redundant. By adding the isAdmin in the conditional you are then telling JavaScript to only pass the alert if the user is an admin.

I hope this helps.

Try if (isAdmin === true). You are notreassigning isAdmin to true, you are testing whether it is true.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Tyler.

You basic syntax is correct; however, there are a couple problems with your conditional and your alert statement.

First, with your conditional - The challenge wants you to check if the isAdmin variable is true. You don't need all the information you have pit into the conditional. Remember, if conditionals will only check if it is True or False, so you only need if (isAdmin). This will check to see if the variable is returning True or False (True in this case because it was preset.).

Second, The challenge did not ask for any HTML tags in the return statement. Challenges are very picky and will only accept what it has asked for, in this case, just return "Welcome administrator."

Here is what the completed and corrected code looks like. Go through the explanations and the code. I hope this makes sense now.

var isAdmin = true;
var isStudent = false;

if(isAdmin) {
  alert("Welcome administrator");
}

Keep Coding! :)

Tyler Darnell
Tyler Darnell
1,886 Points

Yes, thank you, Code Peeps.