First "undefined" then null and now "null"

D

Don Vaillancourt

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: (e-mail address removed) <mailto:[email protected]>
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.
/
 
M

Michael Winter

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
 
V

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".
 
M

Martin Honnen

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).
 
D

Don Vaillancourt

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 said:
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: (e-mail address removed) <mailto:[email protected]>
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.
/
 
M

Michael Winter

[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
 
D

Don Vaillancourt

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 said:
VK wrote:




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: (e-mail address removed) <mailto:[email protected]>
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.
/
 
M

Martin Honnen

Don said:
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).
 
D

Don Vaillancourt

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 said:
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.


--
* Don Vaillancourt
Director of Software Development
*
*WEB IMPACT INC.*
phone: 416-815-2000 ext. 245
fax: 416-815-2001
email: (e-mail address removed) <mailto:[email protected]>
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.
/
 
L

Lasse Reichstein Nielsen

Don Vaillancourt said:
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'
 
V

VK

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


\ | /
( )
| |

Tried to draw a light bulb sign above :)
Mais oui, of course! I should think about it earlier.
 
G

Grant Wagner

Don said:
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.
 
S

Steve van Dongen

Don Vaillancourt said:
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
 
T

Thomas 'PointedEars' Lahn

Steve said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top