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 Boolean Values

James Summers
James Summers
501 Points

can you put a conditional statement inside a conditional statement

I was just wondering if you could put a conditional statement inside of another conditional statement like this: const number = prompt("Guess a number:"); if (+number === 7) { alert("You were right!"); }else { const NuMbEr = prompt("Sorry! Guess again!");

 if (+NuMbEr === 7) { 
alert("You were right!");

}else { alert("Sorry! The number was seven!");

} }

Just wondering.

1 Answer

Steven Parker
Steven Parker
216,083 Points

Sure, you can nest conditionals as deeply as needed. But after several layers it could make your program difficult to read and maintain, so you should consider your logic in case there might be a simpler approach to do the job.

Also remember your other options, such as "else if" and "case" statements.