Javascript Variables’ Accessibility

When I started writing JavaScript (and code in general, for that matter), one of toughest things for me to wrap my head around was what variables and functions I could access from where.

For example, I would try things like this:

The previous code would fail because the variable hadn’t been set yet when it was called. Instead, it should look more like this:

The previous code will work because the variable had been set before it was called. You see, JavaScript is a linear language. Even files have to be loaded in a linear fashion to guarantee that you aren’t calling any variables that are not already set. That’s why if you’re using jQuery, you have to load it into the DOM before you load any scripts that use the jQuery library. I know that’s pretty basic, but it’s the starting point for the rest of this.

How about this example?

That example will also fail because the variable is created inside the function and the variable is being called outside of it. There are a few of ways to access variables that are set inside of functions, however. The code below will work because the variable is already defined outside of the function. The variable is updated inside the function, but it is updated before we alert it and so it is still accessible.

Another way to retrieve variables set inside a function is to return them.

The code above will work because the function doesn’t rely on any global variables and it simply returns the variable that is set inside of it. In this case, you could literally say that MyFunction equals ‘Test.’ You can also return the variable using the ‘this’ associated with the function:

When trying to figure out if a variable will be accessible, ask yourself if it is on the same level (not nested in a function) as the place you are attempting to call the variable or if it is a parent (a function or more higher than the variable you are attempting to call.) If the answer is yes, then you will be able to get to the variable. That’s why something like this won’t work:

But this will:

Any questions?