If I have two separate scripts in an HTML page with JavaScript are the variables shared between the entire page? Or only within their own declarations?
Example:
<script> var title = "Hello World!"; </script>
// random HTML/PHP
<script> document.write(title); </script>
Will that write "Hello World!"? This seems like bad coding convention how else could I achieve something like this with proper form.
They will all be "shared" in accordance with scoping rules and such. Separate files has no effect on this EXCEPT the order of the inclusion of said files.
Edit: The same rule applies to inline scripts as well.
And to elaborate on the exception:
If I have file Foo where I declare the following:
var fooVar = barVar;
Then I have file Bar where I declare the following:
var barVar = 'bar';
And I include them in this order:
You will get a interpreted error because the use of
barVar
comes before its declaration.The
window
holds all variables. All scripts are in the motherwindow
object. So all variables are in one space. They can, however, be localized to functions, etc.Variable title in your example is declared as a global variable, therefore it will be available to any and all scripts loaded into the same page. Whats more, if there is already a global variable named
title
on the same page, its value will be overwritten when you assign it the value "Hello World!"The usual practice to avoid this sort of problem is to declare exactly one global variable, then put all of your other variables inside it. For example:
Assign that lone global variable a name that no one else is likely to choose, such as your name or employer's name or best-of-all, a domain name that belongs you or your employer.
Another, more common way to handle this problem is to take advantage of of the way that JavaScript handles variable scope within functions. For example, create an anonymous function, declare all of your code inside that function, then call the function at the end of the declaration by putting () at the end of the declaration. For example:
If you want to share some variables, but not others, have your anonymous function use a combination of approaches:
One final note. All of the functions that your code declares are also effectively global variables. So, if you create a function named printTitle, it is 1) available to all other code on the page and 2) could overwrite or be overwritten by another function on the same page also named printTitle. You can protect and/or expose your functions the same way you would any other variable:
Note that although function addOne is effectively a private function within the closure, it is still accessible indirectly, via the printTitle function because addOne and printTitle are both within the same scope.
In this case, title would be a global variable. You need to encapsulate the variable within a scope. There are various methods for doing so. My preference is a self-executing anonymous function, which would be done like so:
title
is in theGlobal
scope, which, in the case of JavaScript running in a web browser, is thewindow
object. When you sayvar title = "Hello World!"
outside of any function that would limit its scope, it is the same as sayingwindow.title = "Hello World!"
.Your code is equivalent to this: