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

false or true

var is Admin=true

the task want to me write a code to cek wether: isAdmin variable is true. What does it mean man? I didn't understand logic behind this question

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>

4 Answers

isAdmin is a variable, which means it can "hold" a value in a similar way variables work in maths. Boolean variables are a special type of variable meaning they can only take 2 possible values - either true or false. You probably have heard of binary, where something can either be "On" or "Off", or in other words "Yes" or "No", or otherwise "True" or "False". In binary numbers you have probably have seen this representation as "1" or "0".

In this task, you have the variable isAdmin which is a boolean variable. It can either be true, which would mean that "yes, this user is an admin", or false, which would mean "no, this user is not an admin".

To check in the program whether or not the isAdmin variable is true or false, you have to use a conditional - an if clause. Here is how to do that:

if (isAdmin == true) {
    //do something when isAdmin is true
} else {
   //do something when isAdmin is false
}

Don't forget that you must use double equal-sign here. Single equal-sign assigns a value to a variable, whereas double equal-sign is used when comparing the variable with something else.

The above code can be narrowed down a bit if you want:

if (isAdmin) {
   //do something when isAdmin is true
} else {
  //do something when isAdmin is false

The difference is that I removed == true from the if clause. The fuctionality doesn't change at all though.

If you are wondering "why" this variable is used in the first place, imagine that you have a website that needs to display different options depending on if the user is an administrator of that site or not. Think admin-control-panel or editing-tools that must only be shown to admins, and hidden to normal users.

Hope this helps! Good luck.

Grant G
Grant G
19,934 Points

To check if isAdmin variable true or false, you should use conditional statement. Like this:

if(isAdmin == true) {alert("Welcome administrator")}

Your answer mostly clarified and opened my mind mr! Here appeared another question : can I always take away that equal sign whenever I want or it belong only to boolean variables?

Good question!

Removing the equal sign tells the program to check if that variable is true or false. So you can do this with boolean variables no-problem. You can also do this with numbers for example, but you'll probably get a different result than the one you want.

if you do:

var x = 5;
if (x) {
    alert("test");
}

the if-statement will always be true regardless if x is 0, 1 or 100000, because, well... x is true: it exists/has a value. So other than boolean variables, it would only make sense to use this if you want to check if x was ever set/created.

Thank you Christian very much, I am a Muslim )) I appreciate your help. I think your explanations will be useful not only for me but noobie coders too.

I simply can't understand what it is point to check isAdmin == true , can you clarify this? I mean what is like to be true or false, what is the difference between being false and true, I know that for example 5 < 4 is false, it is clear but myvar=true/false isn't clear for me.