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 trialMichael Abtan
2,409 PointsBooleans can also be treated with 1 and 0.
The attached code should be correct.
if (1) {
alert('This is true');
} else {
alert('This is false');
}
<!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
Marcus Parsons
15,719 PointsHey Michael,
Although 1 is considered a truthy value and 0 is considered a falsey value, they are not strictly boolean values. You have to use the boolean true
in this challenge.
Michael Abtan
2,409 PointsThank you for the quick reply Marcus.
I've figured out the answer I need in order to pass the challenge rather quickly, but I still can't see the difference.
As far as I know, I can use 1
instead of true
anywhere.
For example:
var bool = true;
if (bool == 1) alert (true); //will alert 'true'
Marcus Parsons
15,719 PointsAbsolutely, you can use 1 in place of true in a non strict comparison using ==, which is also what happens when you leave off any comparison operators: you get a non-strict comparison to a truthy value. However, the best practice for comparison is using ===. Also, the challenge explicitly says to use a boolean
value. 1 is not a boolean value.
var bool = true;
//will not alert
if (bool === 1) {
alert('true');
}
Michael Abtan
2,409 PointsI understand now. Thank you for elaborating on this.
Marcus Parsons
15,719 PointsNo problem, Michael. Do some digging on using == and === in JavaScript, and you'll find that any reputable web development source recommends doing a strict comparison when having to compare to a value because == can do some really wonky things. == forces a type conversion whenever it does a comparison, and this can result in some really whacky comparisons.