Im trying to use typeof to determine whether or not a variable is undefined:
function reset_textarea(reset) {
if (typeof(reset) != 'undefined') {
...do stuff
}
}
Im calling it like this:
reset_textarea('hello');
Its not working and I can’t seem to figure out why. The function fires normally if I remove the if statement, thus – the issue seems to lie in the way I am testing whether or not the variable is set. Any idea what is going on?
Well,
typeof("hello")(type of string) is defined. It’s aString.typeof(hello)(note missing quotes) is what you need. Does this work for you?You must understand the difference between a variable (
hello) and a string ("hello"). Remember thatwindow["hello"]is equivalent towindow.hello, but more flexible.