471,310 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

First "undefined" then null and now "null"

What's going on with Javascript.

At the beginning there was the "undefined" value which represented an
object which really didn't exist then came the null keyword. But
yesterday I stumbled across "null" string.

I know that I will get an "undefined" when I try to retrieve something
from the DOM which doesn't exist.

I have used null myself to initialize or reset variables. But in which
case would Javascript return a null or "null".

Are there any other return values out there that I may not know about?

Thanks
--
* Don Vaillancourt
Director of Software Development
*
*WEB IMPACT INC.*
phone: 416-815-2000 ext. 245
fax: 416-815-2001
email: do**@web-impact.com <mailto:do**@webimpact.com>
web: http://www.web-impact.com

/ This email message is intended only for the addressee(s)
and contains information that may be confidential and/or
copyright. If you are not the intended recipient please
notify the sender by reply email and immediately delete
this email. Use, disclosure or reproduction of this email
by anyone other than the intended recipient(s) is strictly
prohibited. No representation is made that this email or
any attachments are free of viruses. Virus scanning is
recommended and is the responsibility of the recipient.
/
Jul 23 '05 #1
13 2830
On Wed, 27 Oct 2004 09:52:53 -0400, Don Vaillancourt <do**@webimpact.com>
wrote:
What's going on with Javascript.
A rather vague question.
At the beginning
At the beginning of what?
there was the "undefined" value which represented an object which really
didn't exist then came the null keyword.
What are you actually describing here?

The value, undefined, is encountered when you would expect it - when
something hasn't been defined.

For example,

alert(window.aNonExistantVariable);

would yield undefined, as would

function myFunction() {
return;
}

alert(myFunction());

because no return value was specified.

The null value is, in my mind, used to signify that there is no object,
when an object reference is expected. For example, document.getElementById
returns a reference to an element within the document. However, if no
element with the specified id can be found, you'll get null instead.
But yesterday I stumbled across "null" string.
[snip]
I have used null myself to initialize or reset variables. But in which
case would Javascript return a null or "null".
There is no built-in or host method that would return the string, 'null',
to signify the result of an operation.
Are there any other return values out there that I may not know about?


Again, a very strange question. There is no "standard set" of return
values. An API function may return anything it says it will in its
specification, and a user-defined function can return anything at all. A
string, a number, an object (user-defined, regular expression, date, or
function), an array, a boolean, undefined, or null.

I doubt that helped, but as I'm not sure what you're asking, such a result
is not too surprising.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
VK
The rules are nearly the same as in other languages.

Undefined value means that the requested object/property was never
initialized (doesn't exist).
window.alert(document.qwertyuiop) > 'undefined'
Thus you cannot assign undefined value - any assignement (even null) makes
an initialization, so it is not undefined anymore.

Null value can be assigned to an object/property. It means what it means -
null, nothing.
var nothing = null;
window.alert(nothing) > 'null';

By their nature undefined and null are very different.
In higher level languages you usually have means to check if a value is
undefined (never was init) or null (explisetly set to nothing). Respectively
you have to use the corresponding comparison methods.
In JavaScript they just simplified the comparison rules, so you can check
the existence of an object like this:
if (obj1.obj2 == null)
which is really should be read something like
if undefined(obj1.obj2)

As a consequence there is no direct way in JavaScript to check against
"never initialized"/"set equal to null".

Jul 23 '05 #3
Well I am talking about the standard implemented methods in Javascript
for both Mozilla and MSIE.

Whenever you try to access a property that doesn't exist Javascript will
return the "undefined" string. That I understand.

I have run into some Javascript methods returning null as well which I'm
okay with and always testing for.

But yesterday I ran into a "null" string which I found odd. I can't
recall which method I was calling to get this result. But I'll look for
it today as I have written a method to test against all three.

Michael Winter wrote:
On Wed, 27 Oct 2004 09:52:53 -0400, Don Vaillancourt
<do**@webimpact.com> wrote:
What's going on with Javascript.

A rather vague question.
At the beginning

At the beginning of what?
there was the "undefined" value which represented an object which
really didn't exist then came the null keyword.

What are you actually describing here?

The value, undefined, is encountered when you would expect it - when
something hasn't been defined.

For example,

alert(window.aNonExistantVariable);

would yield undefined, as would

function myFunction() {
return;
}

alert(myFunction());

because no return value was specified.

The null value is, in my mind, used to signify that there is no object,
when an object reference is expected. For example,
document.getElementById returns a reference to an element within the
document. However, if no element with the specified id can be found,
you'll get null instead.
But yesterday I stumbled across "null" string.

[snip]
I have used null myself to initialize or reset variables. But in
which case would Javascript return a null or "null".

There is no built-in or host method that would return the string,
'null', to signify the result of an operation.
Are there any other return values out there that I may not know about?

Again, a very strange question. There is no "standard set" of return
values. An API function may return anything it says it will in its
specification, and a user-defined function can return anything at all.
A string, a number, an object (user-defined, regular expression, date,
or function), an array, a boolean, undefined, or null.

I doubt that helped, but as I'm not sure what you're asking, such a
result is not too surprising.

Mike

--
* Don Vaillancourt
Director of Software Development
*
*WEB IMPACT INC.*
phone: 416-815-2000 ext. 245
fax: 416-815-2001
email: do**@web-impact.com <mailto:do**@webimpact.com>
web: http://www.web-impact.com

/ This email message is intended only for the addressee(s)
and contains information that may be confidential and/or
copyright. If you are not the intended recipient please
notify the sender by reply email and immediately delete
this email. Use, disclosure or reproduction of this email
by anyone other than the intended recipient(s) is strictly
prohibited. No representation is made that this email or
any attachments are free of viruses. Virus scanning is
recommended and is the responsibility of the recipient.
/
Jul 23 '05 #4


VK wrote:

Undefined value means that the requested object/property was never
initialized (doesn't exist).
window.alert(document.qwertyuiop) > 'undefined'
Thus you cannot assign undefined value - any assignement (even null) makes
an initialization, so it is not undefined anymore.


That is not true, you can certainly do
var x = undefined;
if you want to (unless you happen to have some old JScript
implementation where "undefined is undefined" but with JScript 5.5 or
later that shouldn't happen).
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #5
On Wed, 27 Oct 2004 16:46:00 +0200, VK <sc**********@yahoo.com> wrote:

[snip]
As a consequence there is no direct way in JavaScript to check against
"never initialized"/"set equal to null".


Yes, there is:

1) A strict comparison:

null === ref

2) Using typeof:

'undefined' == typeof ref

The former will only evaluate to true if ref is null. The latter will only
evaluate to true if ref is undefined. If you're concerned about early NN4
support, the latter is preferred.

An alternative to 1) is:

!ref && ('object' == typeof ref)

If ref is undefined or null, the first part of the expression will
evaluate to true, but only null is of type, object.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
I do remember in IE I used to have to define:

undefined = "undefined"

because it didn't exist at the time but in did in Netscape 4.7.
Martin Honnen wrote:


VK wrote:

Undefined value means that the requested object/property was never
initialized (doesn't exist).
window.alert(document.qwertyuiop) > 'undefined'
Thus you cannot assign undefined value - any assignement (even null)
makes
an initialization, so it is not undefined anymore.

That is not true, you can certainly do
var x = undefined;
if you want to (unless you happen to have some old JScript
implementation where "undefined is undefined" but with JScript 5.5 or
later that shouldn't happen).

--
* Don Vaillancourt
Director of Software Development
*
*WEB IMPACT INC.*
phone: 416-815-2000 ext. 245
fax: 416-815-2001
email: do**@web-impact.com <mailto:do**@webimpact.com>
web: http://www.web-impact.com

/ This email message is intended only for the addressee(s)
and contains information that may be confidential and/or
copyright. If you are not the intended recipient please
notify the sender by reply email and immediately delete
this email. Use, disclosure or reproduction of this email
by anyone other than the intended recipient(s) is strictly
prohibited. No representation is made that this email or
any attachments are free of viruses. Virus scanning is
recommended and is the responsibility of the recipient.
/
Jul 23 '05 #7


Don Vaillancourt wrote:
I do remember in IE I used to have to define:

undefined = "undefined"


You should do
var undefined = void 0
but as said that was necessary with older JScript versions and is no
longer in JScript 5.5 or 5.6 (as installed with IE 5.5 or IE 6).

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #8
Now that I remember, I used to test against undefined in Netscape, but
MSIE returned "undefined". Now it seems "undefined" is returned in all
cases.

Don Vaillancourt wrote:
I do remember in IE I used to have to define:

undefined = "undefined"

because it didn't exist at the time but in did in Netscape 4.7.
Martin Honnen wrote:


VK wrote:

Undefined value means that the requested object/property was never
initialized (doesn't exist).
window.alert(document.qwertyuiop) > 'undefined'
Thus you cannot assign undefined value - any assignement (even null)
makes
an initialization, so it is not undefined anymore.


That is not true, you can certainly do
var x = undefined;
if you want to (unless you happen to have some old JScript
implementation where "undefined is undefined" but with JScript 5.5 or
later that shouldn't happen).


--
* Don Vaillancourt
Director of Software Development
*
*WEB IMPACT INC.*
phone: 416-815-2000 ext. 245
fax: 416-815-2001
email: do**@web-impact.com <mailto:do**@webimpact.com>
web: http://www.web-impact.com

/ This email message is intended only for the addressee(s)
and contains information that may be confidential and/or
copyright. If you are not the intended recipient please
notify the sender by reply email and immediately delete
this email. Use, disclosure or reproduction of this email
by anyone other than the intended recipient(s) is strictly
prohibited. No representation is made that this email or
any attachments are free of viruses. Virus scanning is
recommended and is the responsibility of the recipient.
/
Jul 23 '05 #9
Don Vaillancourt <do**@webimpact.com> writes:
Well I am talking about the standard implemented methods in Javascript
for both Mozilla and MSIE.
There are about a gazillion of those, with all kinds of return values.
Do you have a specific example?
Whenever you try to access a property that doesn't exist Javascript
will return the "undefined" string. That I understand.
No, it will return the undefined value. It is a specific value that
satisfies:
typeof <that value> == "undefined"
It is also the only value that satisfies this (it is the only value
of that type).

In modern ECMAScript implementations, there is a global variable called
"undefined" that holds the undefined value.
I have run into some Javascript methods returning null as well which
I'm okay with and always testing for.
Javascript DOM methods are usually specified as returning null when
there is no reasonable return value. Remember that DOM is also defined
for Java where there is no undefined value, and static typing means
that the function must return a value matching some class.
But yesterday I ran into a "null" string which I found odd.
I assume that is a string value (typeof value == "string") with
length four.
I can't recall which method I was calling to get this result. But
I'll look for it today as I have written a method to test against
all three.


If you find it, do tell.

/L 'and please don't top post'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #10
VK
> !ref && ('object' == typeof ref)
\ | /
( )
| |

Tried to draw a light bulb sign above :-)
Mais oui, of course! I should think about it earlier.
Jul 23 '05 #11
Don Vaillancourt wrote:
But yesterday I ran into a "null" string which I found odd. I can't
recall which method I was calling to get this result. But I'll look for
it today as I have written a method to test against all three.


null has typeof "object":

var a = null;
alert(typeof a); // "object"

This probably isn't technically correct, but it's how I view it:

JavaScript does object type-conversion when necessary. The -null- "object"
should be no different. If you use a variable containing -null- in a way that
requires a string, it is type-converted to a string (I just like to think
that toString() will be called implicitly - it can't be called explicitly by
you however) and you get the string consisting of the letters n, u, l and l.

var a = null;
if ("" + a == "null") alert('yes');
if (a == null) alert('yes');

Both alert "yes" because in the first example, -a- was concatenated to an
empty string. JavaScript type-converts the -object- null to the -string- (or
calls toString() on it, or however it achieves the conversion) containing the
character sequence n, u, l, l, which, when compared to another string
containing that character sequence, results in true.

So my suspicion is that you used a variable containing a null value in a way
that required it to be type-converted into a string.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #12
Don Vaillancourt <do**@webimpact.com> wrote:
Now that I remember, I used to test against undefined in Netscape, but
MSIE returned "undefined". Now it seems "undefined" is returned in all
cases.


Nothing returns the string "undefined" to indicate an undefined value.

Regards,
Steve
Jul 23 '05 #13
Steve van Dongen wrote:
Don Vaillancourt <do**@webimpact.com> wrote:
Now that I remember, I used to test against undefined in Netscape, but
MSIE returned "undefined". Now it seems "undefined" is returned in all
cases.


Nothing returns the string "undefined" to indicate an undefined value.


That's not true. The "typeof" operator returns "undefined" if its operand
is an undefined value in both JavaScript 1.1+ and JScript, as specified in
ECMAScript.
PointedEars
--
how many <alt><e>s does it take to end that task?
Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by JKop | last post: by
2 posts views Thread by Mr X | last post: by
1 post views Thread by Pavils Jurjans | last post: by
3 posts views Thread by Mark Sullivan | last post: by
4 posts views Thread by Flip | last post: by
49 posts views Thread by matty | last post: by
9 posts views Thread by Klaus Johannes Rusch | last post: by

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.