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 8282027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:13:59+00:00 2026-06-08T10:13:59+00:00

Can anybody explain, why next js code rises two alert windows with ‘string1’ text

  • 0

Can anybody explain, why next js code rises two alert windows with ‘string1’ text rather than to rise the second with ‘undefined’ text inside?? If both variables are described in the same scope..

var a = 'string1';  
alert(a);  
var a;  
alert(a);​

http://jsfiddle.net/FdRSZ/1/

Thanks

  • 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-08T10:14:01+00:00Added an answer on June 8, 2026 at 10:14 am

    Variable declarations (and function declarations) are hoisted to the top of the scope in which they appear. Assignments happen in place. The code is effectively interpreted like this:

    var a;
    var a;
    a = 'string1';
    

    For example, consider what happens if you declare a variable inside an if statement body:

    console.log(myVar); //undefined (NOT a reference error)
    if (something === somethingElse) {
        var myVar = 10;
    }
    console.log(myVar); //10
    

    Because JavaScript does not have block scope, all declarations in each scope are hoisted to the top of that scope. If you tried to log some variable that was not declared, you would get a reference error. The above example is interpreted like this:

    var myVar; //Declaration is hoisted
    console.log(myVar);
    if (something === somethingElse) {
        myVar = 10; //Assignment still happens here
    }
    console.log(myVar);
    

    So even if the condition evaluates to false, the myVar variable is still accessible. This is the main reason that JSLint will tell you to move all declarations to the top of the scope in which they appear.


    In slightly more detail… this is what the ECMAScript 5 spec has to say (bold emphasis added):

    For each VariableDeclaration and VariableDeclarationNoIn d in code, in
    source text order do

    • Let dn be the Identifier in d.
    • Let
      varAlreadyDeclared be the result of calling env’s HasBinding concrete
      method passing dn as the argument.
    • If varAlreadyDeclared is false, then
      • Call env’s CreateMutableBinding concrete method passing dn and
        configurableBindings as the arguments.
      • Call env’s SetMutableBinding concrete method passing dn, undefined, and strict as the arguments.

    So, if a binding already exists with the identifier we are trying to bind now, nothing happens.

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

Sidebar

Related Questions

Can anybody explain me why below code is working on private member variable? public
Can anybody explain what does JdClient is in this code: public JdClient client =
can anybody explain why uploaded php files treated by a server as a text
can anybody explain why the following bash code involving compound operators is not behaving
Can anybody explain me this piece of code? /* Pad to size of `struct
Can anybody explain why DateTime in IronRuby is Object[] sample code IronRuby 0.9.1.0 on
Can anybody explain the following Perl code for me, please? I think its in
Can Anybody explain why this happen? This is my code in a plugin called
I saw someone's code in View/car.html.haml like followows: %body =yield Can anybody explain me
can anybody explain me the below code for trigger?Please note that the above code

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.