Undefined test does not work in javascript

I get the error 'foo' is undefined. in my script when I test my function with undefined parameter. As I understand it, this should not be.

My call 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?

Note. I read other similar questions before posting this one.

0
javascript
source share
2 answers

You cannot pass a variable that does not exist ( undefined , not null ... which exists) to a function, it tries to get the value foo to pass it when you call

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

... and it is not there, so you get an error only on this line, as in the case with a simpler case:

 alert(foo); 

Now, if you tried to name it as a property of something, then it will be valid, for example:

 alert(window.foo); 

It is then passed undefined because this property is undefined on a known / real object.

+2
source share

I am getting an error too, but it is not related to your code, because you are passing a variable that does not exist for the function. It 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 will be 'testString' is undefined .

0
source share

All Articles