0

I'm getting the error 'foo' is undefined. in my script when i test my function with an undefined parameter. As far as I understand, This shouldn't be happening.

My calling code:

//var foo
var test = peachUI().stringIsNullOrEmpty(foo) ;

My function (part of a larger framework).

    stringIsNullOrEmpty: function (testString) {
        /// <summary>
        ///     Checks to see if a given string is null or empty.
        /// </summary>
        /// <param name="testString" type="String">
        ///     The string check against.
        /// </param>
        /// <returns type="Boolean" />

        var $empty = true;

        if (typeof testString !== "undefined") {
            if (testString && typeof testString === "string") {

                if (testString.length > 0) {
                    $empty = false;
                }

            }
        }
        return $empty;
    }

Any ideas?

Please note. I've had a good read of other similar questions before posting this one.

2 Answers 2

2

You can't pass a variable that doesn't exist (undefined, not null...which does exist) into a function, it's trying to get the value of foo to pass it when you call

var test = peachUI().stringIsNullOrEmpty(foo);

...and it isn't there, so you're getting the error on just that line, just as you would with a simpler case:

alert(foo);

Now if you tried to call it as a property of something, then it'd be valid, for example:

alert(window.foo);

Then undefined gets passed in, because it's an undefined property on a known/present object.

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

5 Comments

Hmmm... When I run through this in firebug though I can actually step into my function. I't only when I hit the line if (typeof testString !== "undefined") that it goes boom.
@James - In that case you have a different issue, where is foo defined in the scope above?
Thats's the essence of my test. I'm tring to cater for a scenario where someone attempts to pass an undefined parameter to the function. Probably overkill i'm sure (I probably shouldn't be hiding errors like that either really)
@James - There's not much you can do about that...if it's set to undefined that's ok, but if it's literally not defined, it'd going to blow up when you attempt to pass it to any function...not much you can do about it, basics of the language.
Cheers for that. The more I think about it the more I reckon that it would have been overkill if possible anyway..... Accepted.
0

I get the error too, but it is is not related to your code, it is because you pass a variable that does not exist to a function. This works:

var foo = undefined;
var test = peachUI().stringIsNullOrEmpty(foo) ;

Btw. the error already tells you that the problem is not in your function, otherwise it be 'testString' is undefined.

1 Comment

You know I'd have never interpreted the error message that way... cheers!

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.