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

James Summers
James Summers
704 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
231,275 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.