In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as:
var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined
null is an assignment value. It can be assigned to a variable as a representation of no value:
var TestVar = null;
alert(TestVar); //shows null
alert(typeof TestVar); //shows object
From the preceding examples, it is clear that undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.
Unassigned variables are initialized by JavaScript with a default value of undefined.
JavaScript never sets a value to null. That must be done programmatically. As such, null can be a useful debugging tool. If a variable is null, it was set in the program, not by JavaScript.
null values are evaluated as follows when used in these contexts:
Boolean: false
Numeric: 0
String: “null”
undefined values are evaluated as follows when used in these contexts:
Boolean: false
Numeric: NaN
String: “undefined”
Filed under: Internet, JavaScript, Programming, Software, Web
Hi all!
You are The Best!!!
G’night
Joey,
I’m wondering if you can help me figure out how to detect the non-existence of an array object without producing an error. Here is the scenario:
I want to extend the functionality of an existing js application (a well know popular tool-tip application) without having to recode the 1000 + pages already linked to the js code page.
I want to define an array with new setting for the tooltips, like size of the box, font face, other variables that are normally set when the js page runs. The script I wrote tries to detect the existence of the array with the preferences I put into it, parses them out and sets them before the div layers are built… works great for pages that have the preference array defined… but the rest of the pages that do not have the array defined produce a js error that the array is not defined.
Why is it that object detection works on html element type arrays but not on user defined arrays?:
if (document.all)
oops, the whole comment was not displayed above..
if (document.all) — this gives no error
if (arrayName) — this gives an error
if (arrayName == undefined)
if (arrayName == “undefined”) — these also give errors.
I hope you understand my dilemma and can give me some insight on what to do. Thanks!
Scarab,
Have you attempted to use the typeof operator to determine the existence of the variable? The typeof operator is placed before an operand and returns a string containing the datatype of the operand. If the operand is undefined, it returns 'undefined.'
Consider the following example:
var aryTest = [1,2,3,4];
if(typeof aryTest == 'undefined')
document.write('not defined');
else
document.write('defined');
In the preceding example, 'defined' is written to the page since aryTest has in fact been defined. If you take away the line where aryTest is set, then 'undefined' will be written to the page.
Please let me know if this answers your question.
For some reason, this code doesn’t work for me:
if (typeof(Page_Validators) != “undefined”)
{
for (i=0;i<Page_Validators.length;i++) {
if(Page_Validators[i].innerHTML.match(’Required’))Page_Validators[i].style.display=”block”;
}
}
The line with typeof produces the error. This is very strange.