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

How is if ( correctGuess === true ) the same as if ( CorrectGuess ) - Boolean Values Video

Guil said that if ( correctGuess === true ) is the same as if ( correctGuess ) so we should write the latter, however I don't understand how they are the same? If the problem starts as a false value, I don't see how it is automatically a true value

3 Answers

Hey Bec Asmar,

To answer your question, I have found a great resource for you. It explains in great detail: https://stackoverflow.com/questions/15393935/boolean-in-an-if-statement

Hope this helps!

Thanks Rabin, I had a look at it and what it seems like to me is JS always assumes the value is true until it's proven otherwise?

Damien Bactawar
Damien Bactawar
8,549 Points

I'm sure that link will clear things up for you but I just wanted to add that I think of it as -

Inside the brackets, there is a logical expression that gets evaluated. Think about (true AND false) it is the same as (false). Think about ((false OR true) AND true) this is the same as (true AND true) which is also the same as (true). The expressions reduce down to one boolean value, either true or false.

I may have complicated things a bit but I just wanted to share how I think of that particular problem.

As I progress through the course, I'm starting to understand it a bit better, however it seemed a bit presumptive that JS marked it as true. Is this because JS always assumes it starts out as true?

Damien Bactawar
Damien Bactawar
8,549 Points
  1. "If the problem starts as a false value, I don't see how it is automatically a true value."
  2. "Is this because JS always assumes it starts out as true?"

How did you declare the variable in the code was it like -

var correctGuess;
var correctGuess = 0;
var correctGuess = "";
var correctGuess = null;
var correctGuess = "Hello";
var correctGuess = 24;
var correctGuess = true;

The top four examples will be interpreted as false in a conditional statement and the bottom three ("Hello", 24, true) will be interpreted as true. (https://www.w3schools.com/js/js_booleans.asp).

Interesting question!

Hey Bec Asmar,

I will give you a few examples:

const a = true;

if ( a === true) {
// Run this code
}

The above code is the same as the code below, you can omit equal operator and you will still get the same results.

const a = true;

if ( a ) {
//Run this code
}

However, if it is false then you can either use equal or Not equal operator. For example:

const a = false;

if ( a === false) {
// Run this code
}

OR

const a = false;

if ( a !== true ) {
// Run this code
}

Hope this helps!

Happy Coding!