Good day, I've stumbled upon a trouble with scopes of variables in JS. I have a function like this
var address = ...; //it may be or may not be defined earlier.
// It is not the only variable passed to the function
function search() {
window.location.search = address;
}
But I need to make sure this variable in defined, so i do this
function search() {
function check(value) {
if(!value) {
value = "1";
}
return value;
}
check(address);
window.location.search = address;
}
But what I get sometimes is undefined,passed to the window.location.search, since the check() does return the "1" properly;
Can you please point out my mistake?
[UPDATE]
I should have said earlier - im just looking for a better way to do it. Sure simple if check will get it right, but the $address variable is not the only one You can picture it like this
totaladdress = address1;
totaladdress += address2;
totaladdress += address3;
totaladdress += address4;
totaladdress += address5;
...
window.location.search = totaladdress;
So I need a function to check them (e.g. check(address1);) and avoid several dozens of IFs to make the code cleaner.
typeof foo === 'undefined'.window.location.search = check(address);? You don't use the result of yourcheckcall anywhere