Linked Questions

2475 votes
31 answers
2.7m views

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 ...
Samuel Liew's user avatar
  • 79.5k
32 votes
4 answers
53k views

How should I test if a variable is defined? if //variable is defined //do this else //do this
boom's user avatar
  • 11.8k
0 votes
3 answers
505 views

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 ...
pimvdb's user avatar
  • 155k
0 votes
1 answer
98 views

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 ...
Hikari's user avatar
  • 4,057
0 votes
1 answer
117 views

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?
sherlock's user avatar
  • 764
0 votes
4 answers
97 views

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 ...
Graphic Equaliser's user avatar
1070 votes
15 answers
1.4m views

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?
Jineesh's user avatar
  • 11.4k
268 votes
1 answer
400k views

Things I’ve tried that don’t seem to work: if(lastName != "undefined") if(lastName != undefined) if(undefined != lastName)
Chuck Le Butt's user avatar
80 votes
6 answers
148k views

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 ...
MeltingDog's user avatar
  • 15.8k
5 votes
7 answers
10k views

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 ...
markzzz's user avatar
  • 48.3k
10 votes
2 answers
12k views

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 ...
user2251919's user avatar
2 votes
3 answers
2k views

Is if(!!object) { // do something if object found } a much more guarenteed way to see if any object is present? if(object) { }
unj2's user avatar
  • 53.8k
4 votes
2 answers
4k views

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....
user avatar
0 votes
4 answers
1k views

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 ( ...
Mad-D's user avatar
  • 4,709
1 vote
2 answers
300 views

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 ...
Racheet's user avatar
  • 740

15 30 50 per page