Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements Write an 'if' Statement

Alfredo Galvez
Alfredo Galvez
4,949 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.

Jason Larson
Jason Larson
7,955 Points

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
4,949 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.

Jason Larson
Jason Larson
7,955 Points

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.