6

I saw this example on mozilla documentation but i don't understand why.

if 0 is a property of trees, i was expecting that trees.0 would return redwood but is a wrong statement. Is a[0] a way to access the 0 property of the array ? In this case a["length"] should also work (logicaly). Can anyone make it clear?

link: developer.mozilla.org

var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
0 in trees;        // returns true
3 in trees;        // returns true
6 in trees;        // returns false
KeyNone
  • 8,745
  • 4
  • 34
  • 51
Munteanu Sergiu
  • 391
  • 1
  • 10

6 Answers6

6

Arrays are effectively Objects with special constraints.

In General, for an object, it doesn't matter if you write obj["key"] or obj.key. That is the reason why a["length"] works.

However - besides the fact, that a number is not a valid identifier - a.0 is not equivalent to a[0]. Since the bracket notation expects a string, an unqoted literal will be treated as name of a variable (which itself should contain a string). I'll demonstrate with a valid identifier:

obj["foo"] = "bar";
obj.foo; // -> bar

var baz = "qwert";
obj[baz] = 5;
obj.baz   // -> undefined
obj.qwert // -> 5
// or
obj["qwert"] // -> 5

An Array can also have "Object" properties (like "length"), which you can set and retrieve. So

trees.foo = "bar"
console.log(trees.foo) // -> "bar"

works. But these properties do not account to the length of the array and are also not accounted for typical methods of arrays like push and pop, etc.

Christoph
  • 50,121
  • 21
  • 99
  • 128
2

The only reason a.0 doesn't work is that a property identifier literal (the 0 part of that) isn't allowed to start with a digit. It's purely a parsing thing.

JavaScript has two ways to access properties: Dot notation with a literal, e.g. foo.bar (which has restrictions), and bracketed notation with a string, e.g. foo["bar"] (which has far fewer restrictions). (This is covered in §11.2.1 of the spec.)

JavaScript's standard arrays aren't really arrays at all in the normal sense of the term (a contiguous block of memory we index into). They're just JavaScript objects plus some special behavior. Array "indexes" are actually property names, and they're strings. (According to the specification; implementations are free to optimize this provided things behave according to the spec.)

So yes, you access the "0" property on an array using a[0] or a["0"]; a[0] being much more common and a bit easier to type. :-)

Because you can use a property using bracketed notation, a["length"] works just fine.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Is a[0] a way to access the 0 property of the array ?

Yes. You can't do a.0, though, because 0 isn't a valid Javascript identifier. (You can do a['0'] if you want, though.) In short, if you couldn't use the property name as a variable name, you can't use dotted notation to access the property.

In this case a["length"] should also work (logicaly).

It works fine.

user2357112
  • 260,549
  • 28
  • 431
  • 505
1

There are two ways to access properties of a JavaScript object

  1. Bracket notation
  2. Dot notation

Bracket notation can be used for all properties, but the dot notation cannot be used for numbers or strings with spaces and other sorts of characters that aren't sufficiently simple. So a.0 is not legal, but a[0] is fine.

The rules for when you can use dot notation are on Page 67 of the ECMAScript Specification. You will notice that the dot-notation can only be used if the property name satisfies the syntax clause IdentifierName.

And by the way, length is a valid identifier name, so both

a['length']

and

a.length

are fine.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0

You access an array by index, which you use a[ 0 ]. For an associative array, or an object you can also use a[ "property" ] or a.property or a->property (highly dependent on the language you code).

Discipol
  • 3,137
  • 4
  • 22
  • 41
0

0 is technically a property of trees there. This also means that trees.0 would work. However, JavaScript identifiers must start with a letter (or one of some special characters such as $ or _) but not with a number. Therefore 0 is not a valid identifier, which makes the whole statement invalid.

length is also a property of trees. It is a valid identifier so trees.length obviously works, but also the other notation trees["length"].

PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58