Best test for undefined?

T

Thomas 'PointedEars' Lahn

Bart said:
Well...

I'm wondering why in the original question

if (x == null) {
doStuff(x);
}

won't do.

Keep in mind that the original question was “What's the ‘best’ way to
determine if a variable is defined?†If "defined" is understood as "does
not throw a ReferenceError exception with message ‘... is not defined’, then
the following applies:

If `x' was not declared a variable (and so that variable was not defined),
evaluation of your comparison would throw a ReferenceError. Not so with
`typeof', but that comes at a price: With `typeof' you cannot determine
whether the property referred to by the /Identifier/ was declared a variable
(a property of a Variable Object with the attribute DontDelete).

If `x' was declared and implicitly initialized with `undefined' --

var x;

-- or `null' --

var x = null;

-- or assigned `undefined', e.g.

var x = void 0;

-- doStuff() would be called anyway with your code as `undefined == null'
(it is a type-converting comparison).

ISTM the only way to determine whether an identifier is available is to try
and catch possible ReferenceError exceptions. And without static code
analysis there appears to be no way of determining if an identifier has been
declared a variable but that variable has not been initialized (as it may
have been explicitly initialized with `undefined' or assigned that value
after initialization, but before the test.)


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <ab37b500-1b97-450c-9498-badd643ac361@j3
5g2000yqm.googlegroups.com>, Sun, 9 May 2010 20:02:32, nick
What's the "best" way to determine if a variable is defined?

I don't recall any problem in testing X for the value undefined by using

var U
// ...
if (X==U) ...

although one must remember not to define U. That should continue to
work even if ECMA 6 redefines the value of an undefined variable.


But, of course, for that the variable must have been defined; only its
value is, by definition, the value undefined - until it is defined.

It was a design fault to give the language an undefined type and an
undefined value, since it is then unnecessarily difficult to talk about
it in English. Instead, non-words should have been used - "taghairm"
would nearly serve; "trygdyl" would do. All who know English reasonably
well will understand that those are not English words.

One cannot test directly whether the variable 'akwidukt' has been
declared, since a simple reference to it will give an error. But
'window.akwidukt' is merely 'undefined' if 'akwidukt' has not been
defined.
 
D

Dr J R Stockton

In comp.lang.javascript message <5dmdnU-R8ZcDbHTWnZ2dnUVZ8ludnZ2d@gigane
... and to use the strict comparison operator.

Is that needed if X cannot be null ?
 
D

Dr J R Stockton

In comp.lang.javascript message <j9mdnbPrgdqQ4mzWnZ2dnUVZ8jKdnZ2d@gigane
No, but that wasn't mentioned anywhere by the OP.

Perhaps he did not think of it : the sort of people who actually use
'null' tend not to ask questions here.

I was wanting to verify my opinion, obtained from the definitive but
complex ECMA 262, that. since I only have actually used it to handle
argument lists with genuinely undefined arguments, I have nothing that
needs fixing.
 

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

Staff online

Members online

Forum statistics

Threads
474,078
Messages
2,570,570
Members
47,204
Latest member
MalorieSte

Latest Threads

Top