473,486 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Detecting existance of a variable

How can I check whether or not a variable exists in a script? I'm
thinking in terms of

if (my variable exists) { myfunction(); }
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #1
3 1352
"Fabian" <la****@hotmail.com> wrote in message
news:bv************@ID-174912.news.uni-berlin.de...
How can I check whether or not a variable exists in a script? I'm
thinking in terms of

if (my variable exists) { myfunction(); }


if(typeof x != 'undefined'){
...
}

Is as near as you can get. But a variable that is declared but has not
yet been assigned a value will also return 'undefined' from a typeof
test. A declared variable could be said to "exist" but in reality this
is probably suitable to your problem.

Richard.
Jul 20 '05 #2
> How can I check whether or not a variable exists in a script? I'm
thinking in terms of

if (my variable exists) { myfunction(); }


It's an ugly hack, but you can use a try...catch block to do it.

try {
var i = myVariable;
// My variable is there...
myFunction();
} catch (exception) {
// My variable is not there
}
HTH,

KC
Jul 20 '05 #3
Richard Cornford hu kiteb:
"Fabian" <la****@hotmail.com> wrote in message
news:bv************@ID-174912.news.uni-berlin.de...
How can I check whether or not a variable exists in a script? I'm
thinking in terms of

if (my variable exists) { myfunction(); }


if(typeof x != 'undefined'){
...
}

Is as near as you can get. But a variable that is declared but has not
yet been assigned a value will also return 'undefined' from a typeof
test. A declared variable could be said to "exist" but in reality this
is probably suitable to your problem.


Thanks. That did the job. I have a javascript page separated into two
files, a data file and a parser file. By checking for the existance of
the variable, it can continue to handle the older version of the data
file.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #4

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

Similar topics

15
2672
by: Jay | last post by:
I'm sure this is a really dumb question, but how do you detect a variable type in Python? For example, I want to know if the variable "a" is a list of strings or a single string. How do I do...
9
1873
by: Josiah Manson | last post by:
Hello. I am very new to Python, and have been unable to figure out how to check if a variable exists or not. In the following code I have made a kludge that works, but I think that it would be...
4
2242
by: lyndon hughey | last post by:
I'm having a problem setting the testing for the existance of a nodes outerxml property. Below I try to test for the existance before I access the property, but I receive a null exception error. ...
0
258
by: Bob | last post by:
I want to find a way to detect the existance of the private member of a particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields()...
4
1340
by: Iain Porter | last post by:
Hi all, I have a usercontrol (a textbox with dropdown calendar that fills the textbox with the selected date) that I implement in a datagrid in a webform. On first loading the page, the datagrid's...
12
8191
by: Thomas Carter | last post by:
Imagine that there is some include file f.h that contains the following line: typedef unsigned int ui32 ; My question is: If I have a C source file F.c that includes f.h, is it possible for...
3
1677
by: Duggi | last post by:
Hi When does a static variable come into life? Well, as far as my knowledge goes, I think it is during the execution of static constructor. Because thats the logic point in the program where the...
0
7105
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
6967
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
7132
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
7180
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
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
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...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.