What is the scope of Javascript variables?

T

Trev

Hi all,
Are variables in javascript local to the page, or to the <script> tags
they are defined within?
Say I had a bit of code as follows:

<HEAD>

<script>

var myVar = MyFunction();

</script>

</HEAD>

could I then use myVar as follows?

<BODY>

<script>

MyOtherFunction( myVar );

</script?

</BODY>
 
L

Laurent Bugnion

Hi,
Hi all,
Are variables in javascript local to the page, or to the <script> tags
they are defined within?
Say I had a bit of code as follows:

<HEAD>

<script>

var myVar = MyFunction();

</script>

</HEAD>

could I then use myVar as follows?

<BODY>

<script>

MyOtherFunction( myVar );

</script?

</BODY>

Yes, all script content (in internal script tags, or included through
script src) is interpreted in one "application" only.

If you define something (variable, function...) in one place of the
page, you can use it from another place in the same page without doing
anything. This causes problems especially in pages built with
server-side controls, because each control must make sure that each
variable is unique. If two controls define a global variable or a global
function with the same name, the last one to be parsed wins. That's why
global variable or functions are a bad idea as soon as the page gets a
little too complex.

HTH,
Laurent
 
T

Trev

Thanks, my friend. I am perplexed though because I have done the
following:

<HEAD>

<script>

var myBoolean=new Boolean(true);

// Do other processing, alter value of myBoolean accordingly

</script>

</HEAD>


<BODY>

<script>

if (myBoolean) //**************
{
// Do other stuff
}

</script>

</BODY>

My browser (Netscape 6) displays an error, saying that "Error:
myBoolean is not defined" at the line marked above with the asterisks.
Whats gone wrong? It all looks fine to me....

TIA

Trev
 
T

Trev

I don't know if this is relevant, but the if statement in the second
script tag is encased within a function:


<BODY>

<script>

function DoMyStuff(){

if (myBoolean) //**************
{
// Do other stuff
}

}
</script>

</BODY>
 
R

Randy Webb

Trev said the following on 10/30/2006 12:51 PM:
Thanks, my friend. I am perplexed though because I have done the
following:

<script>

var myBoolean=new Boolean(true);

var myBoolean = true;

No need for the new Boolean.
// Do other processing, alter value of myBoolean accordingly

if (myBoolean) //**************
{
// Do other stuff
}

My browser (Netscape 6) displays an error, saying that "Error:
myBoolean is not defined" at the line marked above with the asterisks.
Whats gone wrong? It all looks fine to me....

NS6? NS is up to version 8 and 6 is considerably old. It may be a bug in
NS6 that didn't handle new Boolean() <shrug>. Try changing it to just =
true, test it and see what happens. If it still throws the error, insert
alerts to debug it to see where it "disappears" at. And then look into
upgrading your NS :) Firefox is free and NS6/7 are based on Firefox (to
an extent anyway), and NS8 uses two rendering engines - IE and Mozilla.
 
M

Michael Winter

Trev wrote:

[snip]
var myBoolean=new Boolean(true);
[snip]

if (myBoolean) //**************

This is unlikely to achieve what you expect. The variable, myBoolean,
will reference an object. References always evaluate to true when
type-converted to boolean so even:

new Boolean(false)

would pass that test. If you were to insist on using a Boolean object,
you would have to make use of the valueOf method:

if (myBoolean.valueOf())

to obtain the boolean value represented by the object. However, as Randy
pointed out, a boolean value will do.

[snip]

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top