473,418 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 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 3057
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JKop | last post by:
Would you classify the following code as "Undefined Behaviour" or as "Non- portable"? signed main() { signed char chedder = 130; } Relevant information:
2
by: Mr X | last post by:
I'm a few weeks new to JS, and am having a problem. I have a simple INTERNAL style in a web page and I know that style is working. The problem arises when I try to run a script that changes one of...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
1
by: Pavils Jurjans | last post by:
Hello, I am building custom hashtable class, and thinking about value retrieval issues. The thing is, that sometimes the hashtable value may contain value null. If someone is reading this value...
3
by: Mark Sullivan | last post by:
When I trace through a csharp program I came to a situation where a certain values has an "undefined value" as shown in the debugger DbgClr. I want to check this but the following statements did...
4
by: Flip | last post by:
I'm seeing one of my websites giving me an error message on the page after it loads up as "WXBUnit Undefined" What does that mean? The formatting of the site is horrible! I'm not sure what is...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
9
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.