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 JavaScript Basics Making Decisions in Your Code with Conditional Statements Write an 'if' Statement

Alfredo Galvez
Alfredo Galvez
6,145 Points

Assign `message` a value in the conditional statment only.

I get the bummer alert, but I believe I did everything the way it was supposed to. Correct me if im wrong please!

script.js
const isAdmin = true;
const isStudent = false;
let message;

if (isAdmin){ 
   let message = "Welcome admin.";
}

2 Answers

Remove let inside the conditional.

In addition to removing the let inside the conditional, also be sure to remove the period, as that will cause this task to fail.

Alfredo Galvez
Alfredo Galvez
6,145 Points

Alright! thank you both for helping me out! On another matter, why removing the let makes it work? Is it because trying to assign the value to the variable using let would be like creating a new one? Sorry if I'm being silly here.

Yes. The first let message; statement defines the variable with script-level scope. When you use let message = "Welcome admin."; inside the if statement, you are declaring a new variable that only exists until the if statement is done, so if you tried to use the message variable afterwards (like with a console.log(message);, the output would be undefined rather than the expected message because the first message variable is initialized to undefined when it is created, and then it is never updated. By removing the let from inside the conditional, you are then assigning the value "Welcome admin." to the variable message that you created with the let message; statement.