3

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.

2
  • If it may or may not be defined you need to use typeof foo === 'undefined'. Commented May 16, 2013 at 7:13
  • Did you want window.location.search = check(address);? You don't use the result of your check call anywhere Commented May 16, 2013 at 7:43

4 Answers 4

1

When you assign "1" to value it does not modify address because the String reference associated with address is copied into value, and you cannot modify the original reference. Example:

var address = "abc";
var value = address;
value = "def";
// address is still "abc"

You correctly return the string from the function, but the returned value is ignored so it doesn't help. What you want is

address = check(address);
window.location.search = address;

or just

window.location.search = check(address);

or even

function setAddress(value) {
    window.location.search = value || "1";
}
Sign up to request clarification or add additional context in comments.

Comments

1

You cannot use a function for this kind of check if the variable may be undefined - you need to use typeof directly at the place where you use the possibly undefined name:

function search() {
    window.location.search = typeof address !== 'undefined' ? address : 'some default value';
}

1 Comment

That is a vaild option, but the thing is - if i log the variable from within check function - it is correctly displayed as "1" if undefined. The problem is that this "1" is discarded after that and I get undefined again in the end.
0

You re not returning any value from the check method.

function search() {
    function check(value) {
        if(!value) {
            value = "1";
        }
        return value;
    }
    window.location.search = check(address);
}

Comments

0

[UPDATE]Sorry misunderstood what you needed. You can try 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.address = (window.address === undefined)? 1:window.address;
  window.location.search = window.address;
}

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.