What is "variable scope"?

The scope of a variable is the place where it is relevant and accessible. For example, the scope of properties (a kind of variable) is “all its script object”, and the scope of local variables is only “the handler where it resides”.

As this is a vague concept, let’s see it with a live sample:

set foo to "dummy variable"
display dialog foo
something()

on something()
	display dialog foo
end something

Here, the variable called “foo” is a local variable. So, if you run this code, you will see a dialog saying “dummy variable”. Later, the handler “something” will throw an error, since it can’t access the variable “foo” (because “foo” was not declared in its own “scope”).

However, if you declare the variable “foo” as a global or property, its scope will include the entire script object, and it will be accessible from any place within such script object:

property foo : "dummy variable"
display dialog foo
something()

on something()
	display dialog foo
end something

Here you will receive two “dummy variable” dialogs, since the scope of “foo” is available to every part of the script (both “something” and the main script, which is an implicit “run” handler).