Linked Questions
20 questions linked to/from How can I check whether a variable is defined in JavaScript?
2475
votes
31
answers
2.7m
views
JavaScript check if variable exists (is defined/initialized)
Which method of checking if a variable has been initialized is better/correct?
(Assuming the variable could hold anything (string, int, object, function, etc.))
if (elem) { // or !elem
or
if (typeof ...
32
votes
4
answers
53k
views
Test if a variable is defined in javascript? [duplicate]
How should I test if a variable is defined?
if //variable is defined
//do this
else
//do this
0
votes
3
answers
505
views
Check variable whether undefined or doesn't exist [duplicate]
Possible Duplicate:
How can I check whether a variable is defined in JavaScript?
Say we have a piece of code like this. How would one be able to check whether the variable does exist or not in the ...
0
votes
1
answer
98
views
JavaScript breaking even when I test if a variable exists [duplicate]
When PHP is building HTML document, I add to it a code var myVariable = 'somedata';, which is used by a .js file. Of course, that file's code must be prepared for that variable not being set.
Doing ...
0
votes
1
answer
117
views
How can I check if foo exists? [duplicate]
if (foo) {
// code to run if foo exists
}
results in Uncaught ReferenceError: foo is not defined
So how am i supposed to check if foo exists?
0
votes
4
answers
97
views
How to see if a variable name has been defined in Javascript [duplicate]
In Javascript, I define global variables sld_label where label can be any string. These are initialised to objects that have a setval function. I need to check, within a function which is handed label ...
1070
votes
15
answers
1.4m
views
How to check a not-defined variable in JavaScript
I wanted to check whether the variable is defined or not. For example, the following throws a not-defined error
alert( x );
How can I catch this error?
268
votes
1
answer
400k
views
How to check if a JavaScript variable is NOT undefined? [duplicate]
Things I’ve tried that don’t seem to work:
if(lastName != "undefined")
if(lastName != undefined)
if(undefined != lastName)
80
votes
6
answers
148k
views
JQuery/Javascript: check if var exists
Possible Duplicate:
How can I check whether a variable is defined in JavaScript?
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
I have a script that ...
5
votes
7
answers
10k
views
How to init a global variable if it is not present?
I load the same script in my page many times. I have some trouble on decide which is loaded first/after in my website, due to the async/load functions.
So, I'd like to put a global variable that ...
10
votes
2
answers
12k
views
How can I easily set a variable to false with a default of true?
I usually set object properties like so,
// Boolean
this.listening = config.listening || true;
But config.listening is either true or false, and in this case this.listening will always be true ...
2
votes
3
answers
2k
views
Better way to see if an object is present in JavaScript
Is
if(!!object)
{
// do something if object found
}
a much more guarenteed way to see if any object is present?
if(object)
{
}
4
votes
2
answers
4k
views
What's the difference between window.jQuery and if (typeof jQuery == "undefined") when doing fallback?
I want to load jQuery with a fallback solution and have come across two different solutions:
1:
<script src="//ajax.googleapis.com/ [...] jquery.js"></script>
<script> window....
0
votes
4
answers
1k
views
How to check whether the variable is defined or not before assigning in Java Script ?
Here on page load i am trying to store the values from form to variables. Later i would like to use those variables (if and only if i have data in that) to other purpose.
This is how i approached ( ...
1
vote
2
answers
300
views
Is it necessary to use the "typeof" operator to check if a global variable exists?
When I'm writing code that relies on objects created by another script file referenced from the same page I often find myself having to test for the existence of some global variable or another.
I'd ...