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

If Statement

Challenge Task 1 of 1

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".

script.js
var isAdmin = true;
var isStudent = false;
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>

2 Answers

Vrund Patel
Vrund Patel
11,994 Points

In this task you are asked to use conditional if else statements. Basic structure of a if else statement looks something like this.

if ( ) {

}

In the parentheses, evaluates a boolean expression so that if it evaluates to true the code between the curly braces run. So for this task, in the parentheses check if isAdmin true or not. If true, type the code between the braces that will run.

Hope this helps!

Niclas Valentiner
Niclas Valentiner
8,947 Points

To follow up on Vrund's answer:

if ( [expression] ) {
  //Code if expression is true here
} else {
  //Code if expression is false here
}

You can also chain together if else statements by simply starting another if after the else

else if ( [Other expression ){
  //Code if [Other expression] is true here
}