Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #9389 > unrolled thread
| Started by | Tim Streater <timstreater@greenbee.net> |
|---|---|
| First post | 2011-12-18 23:37 +0000 |
| Last post | 2011-12-19 00:48 -0200 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.javascript
Undefined array element becomes defined and null Tim Streater <timstreater@greenbee.net> - 2011-12-18 23:37 +0000
Re: Undefined array element becomes defined and null Stefan Weiss <krewecherl@gmail.com> - 2011-12-19 03:17 +0100
Re: Undefined array element becomes defined and null Tim Streater <timstreater@greenbee.net> - 2011-12-19 09:17 +0000
Re: Undefined array element becomes defined and null Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-19 13:13 +0100
Re: Undefined array element becomes defined and null "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-12-19 16:42 +0000
Re: Undefined array element becomes defined and null "J.R." <groups_jr-1@yahoo.com.br> - 2011-12-19 00:48 -0200
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2011-12-18 23:37 +0000 |
| Subject | Undefined array element becomes defined and null |
| Message-ID | <timstreater-96FB3B.23374518122011@news.individual.net> |
I have a JavaScript array, myArray. I happen to know that let's say
element 27 is undefined - it's never been created. It appears that I can
detect this with:
if (myArray[27]==null)
{
alert (myArray[27]);
}
The alert puts up 'undefined'. Is it valid to be able to detect the
undefined stater of element 27 in this way?
Initially I thought perhaps that the act of accessing the element
created it and set it to null, but apparently not if the alert is
anything to go by. Or is this just a quirk of Safari?
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
[toc] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2011-12-19 03:17 +0100 |
| Message-ID | <WZOdnXmwnYiSAnPTnZ2dnUVZ8u-dnZ2d@giganews.com> |
| In reply to | #9389 |
On 2011-12-19 00:37, Tim Streater wrote:
> I have a JavaScript array, myArray. I happen to know that let's say
> element 27 is undefined - it's never been created. It appears that I can
> detect this with:
>
> if (myArray[27]==null)
> {
> alert (myArray[27]);
> }
>
>
> The alert puts up 'undefined'. Is it valid to be able to detect the
> undefined stater of element 27 in this way?
Not quite. First of all, when you're comparing something to null or
undefined, always use the strict comparison operator:
myArray[27] === null // false
myArray[27] == null // true
The first test fails because myArray[27] actually evaluates to
undefined, and undefined !== null; the second test succeeds because
undefined == null (non-strict comparison).
Neither can tell you if the 28th element of myArray has been set - it
could have been created but set to null or undefined. To find out if the
element exists, you can test for
27 in myArray // false
For the record: if you really only want to find out if myArray[27] is
strictly undefined, but don't care if the index exists or not, you can
do that in several ways
// always safe:
typeof myArray[27] == "undefined"
myArray[27] === void 0
// safe if nobody else can change the "undef" variable:
var undef;
myArray[27] === undef
// built-in "undefined" variable/constant (engine-dependant)
myArray[27] === undefined
> Initially I thought perhaps that the act of accessing the element
> created it and set it to null, but apparently not if the alert is
> anything to go by. Or is this just a quirk of Safari?
There's no such thing as autovivification of array elements or object
properties in JS (thank god).
- stefan
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2011-12-19 09:17 +0000 |
| Message-ID | <timstreater-94380C.09170719122011@news.individual.net> |
| In reply to | #9392 |
In article <WZOdnXmwnYiSAnPTnZ2dnUVZ8u-dnZ2d@giganews.com>,
Stefan Weiss <krewecherl@gmail.com> wrote:
> On 2011-12-19 00:37, Tim Streater wrote:
> > I have a JavaScript array, myArray. I happen to know that let's say
> > element 27 is undefined - it's never been created. It appears that I can
> > detect this with:
> >
> > if (myArray[27]==null)
> > {
> > alert (myArray[27]);
> > }
> >
> >
> > The alert puts up 'undefined'. Is it valid to be able to detect the
> > undefined stater of element 27 in this way?
>
> Not quite. First of all, when you're comparing something to null or
> undefined, always use the strict comparison operator:
>
> myArray[27] === null // false
> myArray[27] == null // true
Righto.
> > Initially I thought perhaps that the act of accessing the element
> > created it and set it to null, but apparently not if the alert is
> > anything to go by. Or is this just a quirk of Safari?
>
> There's no such thing as autovivification of array elements or object
> properties in JS (thank god).
So no magic then - good!
Joao and Stefan - thanks both for a clear summary.
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-12-19 13:13 +0100 |
| Message-ID | <5838947.bgypaU67uL@PointedEars.de> |
| In reply to | #9392 |
Stefan Weiss wrote:
> Tim Streater wrote:
>> I have a JavaScript array, myArray. I happen to know that let's say
>> element 27 is undefined - it's never been created. It appears that I can
>> detect this with:
>>
>> if (myArray[27]==null)
>> {
>> alert (myArray[27]);
>> }
>>
>>
>> The alert puts up 'undefined'. Is it valid to be able to detect the
>> undefined stater of element 27 in this way?
>
> Not quite. First of all, when you're comparing something to null or
> undefined, always use the strict comparison operator:
>
> myArray[27] === null // false
> myArray[27] == null // true
>
> The first test fails because myArray[27] actually evaluates to
> undefined, and undefined !== null; the second test succeeds because
> undefined == null (non-strict comparison).
>
> Neither can tell you if the 28th element of myArray has been set - it
> could have been created but set to null or undefined. To find out if the
> element exists, you can test for
>
> 27 in myArray // false
Property names are strings. To avoid implicit type conversion, use
"27" in myArray
(This points out the unintuitiveness of the `in' operator in ECMAScript.
Not knowing anything about the programming language, you would reasonably
expect this to be true if, and only if, the value 27 or "27" was *contained*
in the array.)
> For the record: if you really only want to find out if myArray[27] is
> strictly undefined, but don't care if the index exists or not, you can
> do that in several ways
>
> // always safe:
> typeof myArray[27] == "undefined"
> myArray[27] === void 0
>
> // safe if nobody else can change the "undef" variable:
> var undef;
> myArray[27] === undef
>
> // built-in "undefined" variable/constant (engine-dependant)
> myArray[27] === undefined
It should be noted that ECMA-262 Ed. 5 finally abolished the overwritable
`undefined' property of the global object. From ECMA-252 Ed. 5.1:
| 15.1.1.3 undefined
|
| The value of |undefined| is *undefined* (see 8.1). This property has the
| attributes { [[Writable]]: false, [[Enumerable]]: false,
| [[Configurable]]: false }.
Google V8 as of Chromium 15.0.874.121 (Developer Build 109964 Linux) is not
conforming in that regard as `undefined = false' sets `undefined' to
`false'. The same applies to `NaN' and `Infinity'.
PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exjxw.hannivoort@interxnl.net> |
|---|---|
| Date | 2011-12-19 16:42 +0000 |
| Message-ID | <Xns9FC0B4361457Eeejj99@194.109.133.133> |
| In reply to | #9407 |
Thomas 'PointedEars' Lahn wrote on 19 dec 2011 in comp.lang.javascript:
> It should be noted that ECMA-262 Ed. 5 finally abolished the
> overwritable `undefined' property of the global object. From ECMA-252
> Ed. 5.1:
What is the sense in making THE global object undefined,
except to render the script useless?
What would happen to your
window.alert('nonsense');
?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "J.R." <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2011-12-19 00:48 -0200 |
| Message-ID | <jcm8li$ggp$1@speranza.aioe.org> |
| In reply to | #9389 |
On 18/12/2011 21:37, Tim Streater wrote:
> I have a JavaScript array, myArray. I happen to know that let's say
> element 27 is undefined - it's never been created. It appears that I can
> detect this with:
>
> if (myArray[27]==null)
> {
> alert (myArray[27]);
> }
>
>
> The alert puts up 'undefined'. Is it valid to be able to detect the
> undefined stater of element 27 in this way?
>
> Initially I thought perhaps that the act of accessing the element
> created it and set it to null, but apparently not if the alert is
> anything to go by. Or is this just a quirk of Safari?
In this particular case, we should use the strict Equals Operator (===)
than just the Equals Operator (==), because undefined == null, for
instance, produces true, whereas undefined === null yields false.
Note: Douglas Crockford's advice is to never use [what he calls] the
evil twins (== and !=). Instead, we should always use === and !==,
although Crockford's advice is a tad exaggerated when dealing with the
typeof operator which always returns a string value.
Another important thing: if we access a missing array element, we will
get the undefined value, not null. So, the OP's code might be rewritten to:
var missingElem = myArray[27];
if (typeof missingElem == 'undefined') {
alert('this is a missing element in myArray');
}
--
Joao Rodrigues (J.R.)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web