0

Well, I am running the following function in my code and wondering why getting an error in Firebug which is as follows:

function TestSurvey(UniqueID, PhontTypes) {

            if(typeof(PhontTypes) != "undefined")
                PhontTypes = "4";

            if (PhontTypes.match("5")) 
            {
                window.location = some url location here...
            }
            else
            {           
                window.location = some url location here...                 
            }

    }

The error I am getting in firebug is as follows:

TypeError: PhontTypes is undefined

if (PhontTypes.match("5"))
6
  • Well, is PhontTypes undefined? Run a console.log(PhontTypes) right before your logic. Commented Nov 13, 2013 at 19:16
  • 5
    Perhaps try == "undefined" rather than != "undefined". As you have it, everything but undefined is replaced with "4". Commented Nov 13, 2013 at 19:16
  • You mean I should change if(typeof(PhontTypes) != "undefined") to if(typeof(PhontTypes) !== "undefined") ? Commented Nov 13, 2013 at 19:19
  • @Jack The way your code currently works: in the event that PhontTypes is undefined, it stays undefined. In the event that it is defined, it is replaced by "4". You want to reverse that behavior. Commented Nov 13, 2013 at 19:20
  • @Jack The problem is the "not" in !=. You want to check for equality rather than inequality. "if PhontTypes is undefined, set it to "4" instead." Commented Nov 13, 2013 at 19:21

1 Answer 1

1

The typeof condition seems to be inverted. It's currently limiting PhontTypes to be only undefined or "4" -- nothing else.

To have it replace undefined with a "default" value of "4", it should check for equality (== or ===) instead:

// if currently `undefined`, set default
if(typeof PhontTypes == "undefined")
    PhontTypes = "4";

Or, since the next line expects String.prototype.match() to be available, you might replace all values that aren't Strings:

if (typeof PhontTypes != "string")
    PhontTypes = "4";
Sign up to request clarification or add additional context in comments.

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.