Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:34:42+00:00 2026-06-18T09:34:42+00:00

I apologize in advance, this might have been discussed on StackOverflow before, I just

  • 0

I apologize in advance, this might have been discussed on StackOverflow before, I just do not know what this is called, so I could not find a satisfactory answer.

However, I am learning JavaScript and with a book called “Eloquent JavaScript”. There I found the following piece of code, which repeatedly prompts the user to enter his name until he did it.

while (!input) {
    var input = prompt("Who are you?");
}

I simply do not understand why this actually works instead of raising an error. At the time the condition expression is being evaluated, there is no variable called input out there. If I understand it right, there is no evaluation possible, which would normally prevent further execution. The statement in the while loop’s body, which then creates a variable called input, is still being executed, though.

However, this made me anxious, so I tried this:

while (!bool) {
    console.log("Hi");
    var bool = true;
}

This is even weirder. It’s same problem when it comes to the condition expression: bool is being created within the scope of the loop’s body, after the evaluation of the condition. And secondly, bool is constantly set to be true, but still the code is being executed once, in other words, Hi is being printed once.

I am confused and would appreciate some help. 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:34:43+00:00Added an answer on June 18, 2026 at 9:34 am

    This is one of Javascript’s little eccentricities. The key thing to remember is that, in Javascript, variables are not necessarily created at the point in the code where var x = appears. They are always created at the top of the scope. This means at the beginning of the function that contains them, or the top of the global scope if that’s the scope we’re working in. This is called hoisting.

    So your code may look like this:

    function doStuff() {
        while (!bool) {
            console.log("Hi");
            var bool = true;
        }
    }
    

    But it will run like this:

    function doStuff() {
        var bool = undefined;
        while (!bool) {
            console.log("Hi");
            bool = true;
        }
    }
    

    (Remember that there is no such thing as block scope in Javascript.)

    The first time, bool is undefined. !undefined is true, so the conditional passes. After that, bool is true, so the conditional fails. It is a similar story with input in your first example.

    The variable will always be created at the very top of the scope, no matter where it is declared. For this reason, it is sometimes recommended as good practice to declare your variables at the top of the scope, since that is where Javascript will consider them to be. This prevents surprises like the one you cite.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might have been dealt with, so I apologize in advance for that. Anyway,
I apologize in advance if this has been addressed, but I have not found
This question might be very abstract, so apologize in advance. I have a log
I apologize in advance for asking this question, I know similar questions have already
I apologize in advance for not knowing how to better state this question. In
This is a very noobish question, so I apologize in advance! I have two
This might be a bit hard to comprehend so I apologize in advance if
I'm a python newbie so I apologize in advance if this question has been
I apologize in advance if folks think this has been beaten to death. I've
So I apologize if this has come up before, but I couldn't find a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.