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 trialLogen Li
Courses Plus Student 1,276 PointsProblem with the "let code" in the video instruction
It seems like Andrew wrote a code:
if (person.role)
is that a mistake because person.role = "Teacher" which is not a boolean result
4 Answers
Mike Francois
11,438 PointsIf statements check to see if a condition is true. In this case (person.role) is the condition. The if statement does not return the contents of person.role which is a string. It checks to see if (person.role) is TRUTHY, meaning it is not set to: false, 0, an empty string or null and it is not undefined or NaN. Since it has a value of 'Teacher', the condition is then TRUTHY so the block of code in the if statement is executed.
The main thing is the if statement does not care what the actual value of person.role is. For example, you can set role 55544433234 and the block of code will still run. Try setting person.role to true and see what happens.
I hope this clears it up.
Mike Francois
11,438 Pointsif(person.role) means if the person object has a role value. In this case, it is a string "Andrew". Therefore the if statement is true. Since it is true, it will perform the operation on the description variable.
Hope that helps.
Logen Li
Courses Plus Student 1,276 PointsWait the role should have a value of βTeacerβ not βAndrewβ right?
Mike Francois
11,438 PointsYes. sorry.
Logen Li
Courses Plus Student 1,276 PointsEmmm. But I do the experiment in the chrome console. When I type person.role, it still returns a string. Then why it return a Boolean value in the if statement
Mike Francois
11,438 PointsYou're Welcome.
Logen Li
Courses Plus Student 1,276 PointsLogen Li
Courses Plus Student 1,276 PointsThank you so much Mike! Appreciate!