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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:24:01+00:00 2026-06-08T01:24:01+00:00

I found a bug in my site, because of this strange behavior. The second

  • 0

I found a bug in my site, because of this strange behavior.
The second function alerts “undefined” instead of “one”.

function test() {
    var myParam = "one"
    if (false) {
        var myParam = "two";
    }
    alert(myParam);
}
test(); // alert "one"


function testAjax() {
    var myParam = "one";
    $.ajax({
        url:    "http://url-to-get-error",
        error:  function(){
            if (false) {
                var myParam = "two";
            }
            alert(myParam);
        }
    });
}
testAjax(); // alert "undefined"

And if I comment block bellow, alerts “one” correctly.
Someone knows why?

if (false) {
var myParam = "two";
}

See in http://jsfiddle.net/EaW8D/2/.


UPDATE

Guys, I know how to to solve the problem, thanks a lot!

But the question is WHY js is setting undefined in a block
that NEVER run (if (false)).

  • 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-08T01:24:05+00:00Added an answer on June 8, 2026 at 1:24 am

    Someone knows why?

    You are re-declaring myParam inside the error handler, shadowing the variable with the same name in the outer scope. Variable declarations are “hoisted” to the top of the function, so your code is effectively the same as:

    error:  function(){
        var myParam;  // <- declaration
        if (false) {
            myParam = "two"; // <-  initialization
        }
        alert(myParam);
    }
    

    and since the if block is never executed, myParam will be undefined (the default value for uninitialized variables).

    Here is a simple example to proof this behaviour. If the variable was not hoisted, foo would become a global variable (assuming non-strict mode) and a property of window, but it does not:

    (function() {
        if(false) {
            var foo = 'bar';
        }
        foo = 'baz';
        console.log(window.foo); // yields `undefined`
    }());
    

    To solve this problem, simply remove var, so that myParam refers to the variable declared in the outer scope, testAjax:

    error:  function(){
        if (false) {
            myParam = "two";
        }
        alert(myParam);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Found this rather strange bug in IE8; element.style.top is limited to 1342177 pixels. Even
I just found a bug in one of the rake tasks shipped with Rails.
I recently found and fixed a bug in a site I was working on
I found a bug in a site that i have created in IE7. URL
I have a Django site where a strange bug is occurring. On the site
I found solution on this site(How can I input separator between items in an
I found a bug on the function below. When temp = 10. It will
::UPDATE:: LINKS DO NOT EXIST ANYMORE! Very strange indeed, this is definitely a bug!
I found a bug on web.py utils.safeunicode function. the version is 0.36 >>> import
I found a bug, and tracked it down. You can see a simplified example

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.