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.