0

Something wierd is happening in my javascript and i dont understand it. Can anyone explain?

var adsl2pSpeed = '9500 - 12500';

        alert(adsl2pSpeed);

        if (!adsl2pSpeed) {
            alert(adsl2pSpeed);
            var adsl2pSpeed = 'Unknown';
        }


        var speed = document.getElementById("PredictedSpeed");
        speed.innerHTML = adsl2pSpeed + " b/s";

This alerts "Undefined" twice and sets the innerhtml to be "Unknown". If I comment out the if statment it alerts '9500 - 12500' and sets the innerHTML to be '9500 - 12500'. Whats happening? Is the string is being cast as an object so it becomes null?

EDIT : I am actually registing the adsl2pSpeed as a startup script not in the function. I moved it up for clarity but possibly that is the problem?

6
  • 1
    Testcase please. Looking at the indentation the real code has stuff between the first two lines... Commented Sep 13, 2011 at 8:57
  • Is that your exact code? Or is something in a function? Commented Sep 13, 2011 at 8:58
  • something else is going on. This fiddle works ok... jsfiddle.net/2jz9k What browser are you using? What is your markup? Do you have any funky libraries loaded etc...? Commented Sep 13, 2011 at 8:59
  • Doesn't for me: jsfiddle.net/v6D9r. Show us the rest of the code. BTW, you don't need the var before var adsl2pSpeed = 'Unknown';... the variable adsl2pSpeed has already been declared. Commented Sep 13, 2011 at 8:59
  • @Matt: I think that var is causing the problem because the indented code is in its own function (which Tom did not tell us about), and the var makes a new local variable. Commented Sep 13, 2011 at 9:01

2 Answers 2

5

Speculation:

The indented code is in a function.

   if (!adsl2pSpeed) {
        alert(adsl2pSpeed);
        var adsl2pSpeed = 'Unknown';
    }

You are in that function declaring a local variable, which masks the global variable, so it looks "undefined".

Try to remove the var to avoid making a new variable.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Would seem like that's the most obvious cause. Would help if the OP mentioned that (if that is indeed the case...)
0

I think Thilo is right. If the code is in a function like:

var adsl2pSpeed = '9500 - 12500';

function test() {
 alert(adsl2pSpeed);
 if (!adsl2pSpeed) {
   alert(adsl2pSpeed);
   var adsl2pSpeed = 'Unknown';
 }
 var speed = document.getElementById("PredictedSpeed");
 speed.innerHTML = adsl2pSpeed + " b/s";
}

// some code runs ...  

test();

then the declaration of adsl2pSpeed inside the if-statement is "hoisted" to the top of the function, so that the function actually is interpreted like this:

function test() {
 var adsl2pSpeed;  // declaration hoisted to the top, shadows the global var
 alert(adsl2pSpeed);
 if (!adsl2pSpeed) {
   alert(adsl2pSpeed);
   adsl2pSpeed = 'Unknown';   // assignment to local var
 }
 var speed = document.getElementById("PredictedSpeed");
 speed.innerHTML = adsl2pSpeed + " b/s";
}

This article explains it: http://bustingseams.blogspot.com/2009/08/another-javascript-pitfall-hoisting.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.