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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:09:42+00:00 2026-06-15T15:09:42+00:00

Possible Duplicate: Redeclare JavaScript Variable I have the next piece of code: var i

  • 0

Possible Duplicate:
Redeclare JavaScript Variable

I have the next piece of code:

var i = 11;
alert(i);
function a(){
    alert(i);
    var i = 2;
    alert(i);
}
a()

The second alert(i) (inside the function) produces undefined. I’m guessing it has to do with the way JS engine runs through the code – maybe it doesn’t store variables first, before going throught the lines?

Anyway I thought that this is not a problem is JS because it supports hoisting. I probably got it wrong – does anybody care to explain?

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-15T15:09:43+00:00Added an answer on June 15, 2026 at 3:09 pm

    JavaScript does indeed hoist declarations to the top of the scope in which they occur, but assignments happen at the place you would expect them to. Your code is effectively parsed like this:

    /* Function declarations are hoisted first, which is why you can invoke a
       function before it appears to be defined in the source */
    function a() {
        var i; // Declaration is hoisted (this i shadows the outer i)
        alert(i);
        i = 2; // Assignment to local i happens in place
        alert(i);
    }
    var i; // Declaration is hoisted (at this point, i === undefined)
    i = 11; // Assignment happens in place
    alert(i);
    a();
    

    This is detailed in the spec. Upon entering a new execution context, the following happens:

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: Removing a function at runtime in PHP I have a script that
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: What is the difference between a function expression vs declaration in JavaScript?
Possible Duplicate: Why does ReSharper want to use 'var' for everything? I have ReSharper
Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: Subtract days from a date in javascript I have got a JavaScript
Possible Duplicate: Operator overloading I am seeing this in a piece of sample code:
Possible Duplicate: Can main function call itself in C++? I found this problem very

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.