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 Loops Working with 'for' Loops Exit a Loop

status === null ; why doesn't it stop the function when I don't submit anything?

const tMinus = 10;
let to = 'Liftoff! 🚀';

for ( let i = tMinus; i >= 1; i-- ) { 
  let status = prompt(`T-Minus ${i}... Continue? (Y/N)`);
  if ( status === null || status.toLowerCase() === 'n' ) {
    to = 'Abort launch!';


In the if ( status === null || status.toLowerCase() === 'n' ) line, the countdown continues even if no letter is input, and also if pure jibberish is input. I suppose there needs to be an if loop for the jibberish part. But why does the loop still run if nothing is put in? Perhaps, I need a better definition for null. I thought that meant nothing is put in or 0. The only time it aborts is with an n or I hit escape or cancel.

3 Answers

I'm not 100% certain but my understanding is as follows...

  • null is an empty or non-existent value.
  • null must be assigned.

By not declaring a value to a variable, JavaScript automatically assigns the value to undefined. JavaScript will never automatically assign the value to null. That must be done by you in your code. However, when you assign null to a variable, you are declaring that this value is explicitly empty.

So leaving the prompt blank does not result in null being the value, because you haven't explicitly stated it to be null. It instead becomes either undefined or possibly just an empty string?


null === undefined // false
null == undefined // true
null === null // true
"" == null // false
"" == undefined // false

I think break tells the program to stop.

An empty prompt response actually returns "" (an empty string), so if you change null to "" in your if statement, it should work. Hope this helped!