Depends on what you mean by "initialized". Going from the examples you
give, it looks like you call an element "initialized" when its index is
less than the array's length.
Right. More formally: an element that is located within the currently
defined array borders. This is what reported as Empty (as opposed to
Undefined or Null) in Visual Basic and VBA.
That may be significant in other
languages, but it isn't in JavaScript. The .length property, whether set
explicitly via 'arr.length' and 'new Array(n)', or implicitly by
assigning to arr[largerNumber], has nothing to do with creating or
initializing any indices (properties) in the array.
It is true for any language. I am not aware of a language that on say
its analogy for
new Array(4294967294)
would loop 4,294,967,294 times making some preliminary
initializations
Case in point: your example in b), new Array(100), does not create all
the elements from 0 to 99, it only sets the .length property of the
array. That's all. arr[10] evaluates to undefined for the same reason
that arr["ten"] would: the property doesn't exist.
Once I had a discussion on it in this group. It all depends on the
level of abstraction one chooses. One can look at Array as an array.
One can look at it as a regular object with a fancy auto-updating
property added. One can say that
var arr = new Array(100);
creates an array with 100 elements in it which are all initialized but
not assigned.
The other can say that that creates a generic Object, sets its
prototype to Array prototype and sets its auto-updating property
"length" to 100.
Both are right but IMO the second one doesn't exactly speak about
Javascript: she is one level lower than needed.
It's just like with types in Javascript. Javascript is a dynamically
typed ("loosely typed" as haters call it) language. That means that
any variable at some given moments has certain type but this type can
be dynamically changed by assigning value of another type. That is
different from VBScript which is typeless or more exactly any variable
is of the same universal type Variant.
Yet an XPCOM programmer will say that in Gecko's JavaScript all
variables are of the same universal type, just like in VBScript. She
will be right as well but from her level which is again one level
lower than needed to talk about a programming language as about a
programming language per se.
An index (property) will only exist if you create it by assigning
something to it. This can be done by assigning to arr[n], or by giving
it a value in an array literal.
(There's also the alternative Array constructor form with more than one
argument, but the less said about that the better. I suppose
Object.defineProperty() might also be used, but I can't test that at the
moment).
None of this has much to do with the length of the array, except that it
will be automatically adjusted behind the scenes when you assign to an
index greater or equal to the value of the .length property.
So you prefer to talk from behind the scene and I do on the scene
It may help to keep the difference between indices (=properties) and
values in mind. Remember, undefined is a value, too. In your last
example, something like 'arr[3] = undefined' would actually increase the
length of the array instead of removing an element.
As I said, my problems with
var x = undefined;
and the like are epistemological and not technical. I do understand
the programmatic outcome yet I refuse to take as sensual "variable
with assigned value 'variable has not been assigned a value'".
The elision syntax is a special case. IMHO, elisions are unintuitive and
generally not very useful. I think it's best to avoid them altogether.
They are definitely a very rare bird, just like named for{} loops. Yet
it might be a situation when one needs to create (automatically server-
side for instance) sparse array with gaps here and there and to make
sure that forEach will work only with assigned array members. Then she
needs to know that elisions will do the work
[0,1,2,3,,,6,,8]
and undefined will not
[0,1,2,3,undefined,undefined,6,undefined,8]