hi, i'm wondering why is it javascript doesn't seem to accept null value as a real null? In order to know if its a null field later on, it's as if i have to check it against a string 'null'.

for example,
i have a txtName hidden field on my html.

I use javascript to document.myForm.txtName.value = null;

later on, when i try to detect it just doesn't work :

if(document.myForm.txtName.value == null){
alert('value is null');
}else if(document.myForm.txtName.value == 'null'){
alert('value is a string null'); //only this works!
}

use a variable.

var test = document.myForm.txtName.value;
test = null;

if(test == null){
alert('value is null');
}else if(test == 'null'){
alert('value is a string null'); //only this works!
}

i think this should work, but i didn't test it.

use a variable.

var test = document.myForm.txtName.value;
test = null;

if(test == null){
alert('value is null');
}else if(test == 'null'){
alert('value is a string null'); //only this works!
}

i think this should work, but i didn't test it.

nope, don't think it'll work. I actually rewrote the sample when i started the thread, i'm actually assigning it to a variable already.

this should be something common, yet no one's talking about it??

As long an HTML input element exists, it's value can never be null. It can be a blank string (string of length zero), but not null. And if that element doesn't exist, it gives you an 'undefined' instead but never null.

<html>
<body id="bdy">
	<form action="#" name="frm">
		<input id="txt" name="txt" />
	</form>
	<script>
		alert(document.forms['frm'].elements['txt']);
		alert(document.forms['frm'].elements['ok']);
	</script>
</body>
</html>

So, to check whether an element exists or not:

if(typeof(document.forms[0].elements['elementName']) === 'undefined')
{
   // an element by the name of 'elementName' doesn't exist
}

Also keep in mind that the DOM function getElementById() returns a 'null' when an element is not found in the DOM tree.

var e = document.getElementById('elementId');
if(e == null)
{
   // an element by the id of 'elementId' doesn't exist
}

Every primitive value in Ecmascript is one of the following types: Null, Undefined, Number, Boolean and String. Null is a type in Ecmascript having only one value, null. The details are implementation specific. Calling it a pointer would be wrong.

This article has been dead for over six months. Start a new discussion instead.