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 Booleans

How do I use booleans with conditional statements in javascript?

I didn't see this addressed anywhere in the video or online.

script.js
var btrue = new Boolean(true);

if (btrue == true) {
    alert('This is true');
} else {
    alert('This is 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>

3 Answers

James Kim
James Kim
8,475 Points

I really don't understand what you are saying, but your code seems correct, but this is something that I would do

var btrue = true;
if(btrue === true){
    console.log("this is true");
}else{
    console.log("this is not true");
}

and it would return the string "this is true"

adding new Boolean(true) seems redudant when you can simply just add true value to the variable btrue.. Also i would use three equal signs "=" to make it strictly equal to true.

Hope that helps!.

Joel Hernandez
seal-mask
.a{fill-rule:evenodd;}techdegree
Joel Hernandez
Front End Web Development Techdegree Student 2,571 Points

You're about to learn a lot more on how to incorporate them. But a boolean value is true by default and a conditional statement can check to see if something is true or not. Such as, has a user inputted text into a text field? If True, let the user continue . If false, deny the form.

In other words, a boolean is always either true or false and you can use them to check if a statement has passed (True) or failed (False).

conditional statement use booleans values to test for true or false statements. this is most seen in loops. including while, do while, if, if else and else. etc. for example a while loop in context would look like this.

while (conditional statement =true) {
  //perform this code until statement
  evaluates to false
} 

// so I could so something like this

var counter = 0;

 while (counter <= 5) {
console.log(' I am counting to ' + counter);
counter += 1;
 }

So I have a variable named counter with a value set to zero, Then I created a while loop that checks the conditional statement to see if that statement evaluates to true, as long as that conditional statement evaluates to true the code in the block will run. in this case the loop checks to see if the variable counter is less or equal to 5, (is 0 less than or equal 5: true) so my code block runs and string is printed to the console which would be, ' I am counting to 0 ', then my code adds 1 to the variable counter. Then the loop checks the conditional statement again, This continues until the conditional statement evaluates to false which in this case would be when the variable counter holds the value of 6 (is 6 less than or equal to 5: false) so the program ends