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 Getting Started With ES2015 Defining Variables With let and const Defining a Variable with let

Problem 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

If 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.

Thank you so much Mike! Appreciate!

if(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.

Wait the role should have a value of β€œTeacer” not β€œAndrew” right?

Yes. sorry.

Emmm. 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

You're Welcome.