473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

What is the scope of Javascript variables?

Hi all,
Are variables in javascript local to the page, or to the <scripttags
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>

Oct 29 '06 #1
5 1250
Hi,

Trev wrote:
Hi all,
Are variables in javascript local to the page, or to the <scripttags
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 29 '06 #2
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

Oct 30 '06 #3
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>

Oct 30 '06 #4
Trev said the following on 10/30/2006 12:51 PM:
Thanks, my friend. I am perplexed though because I have done the
following:
<snip>
<script>

var myBoolean=new Boolean(true);
var myBoolean = true;

No need for the new Boolean.
// Do other processing, alter value of myBoolean accordingly
<snip>
if (myBoolean) //**************
{
// Do other stuff
}
<snip>
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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 30 '06 #5
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
Oct 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hal Vaughan | last post by:
Being self taught, this is one thing I've always had trouble with -- I finally get it straight in one situation and I find I'm not sure about another. I have a class that keeps calling an...
56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
3
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
1
by: Robert North | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've just started programming JS, and one question keeps nagging me: I can insert a <script> element anywhere in HTML, and when it's executed...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
16
by: SirG | last post by:
I'm looking for an explanation of why one piece of code works and another does not. I have to warn you that this is the first piece of Javascript I've ever written, so if there is a better way or a...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
27
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.