Like 0 is a property of trees when var trees = new Array ("redwood", "bay", "cedar")

I saw this example in the mozilla documentation, but I don't understand why.

if 0 is a property of trees, I expected trees.0 return redwood , but would be an incorrect statement. Is a[0] way to access property 0 of an array? In this case, a["length"] should also work (logically). Can anyone understand this?

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 
+6
source share
6 answers

Arrays are objects with special restrictions.

In general, it does not matter for the object whether you write obj["key"] or obj.key . This is why a["length"] works.

However, in addition to the fact that the number is not a valid identifier, a.0 not equivalent to a[0] . Since the notation in parentheses expects a string, an unquoted literal will be considered as the name of the variable (which itself must contain the string). I will 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 (for example, โ€œlengthโ€) that you can set and get. So

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

works. But these properties do not take into account the length of the array and also do not take into account the typical methods of arrays, such as push and pop, etc.

+6
source

The only reason a.0 doesn't work is because the property literal identifier (part 0 ) cannot start with a digit. This is a purely analyzing thing.

JavaScript has two ways of accessing properties: Spot with a literal, for example. foo.bar (which has limitations) and parenthesized notations with a string, for example. foo["bar"] (which has much less restrictions). (This is described in ยง11.2.1 of the specification.)

Standard JavaScript arrays are generally not arrays in the normal sense of the word (the continuous block of memory that we are indexing). These are just JavaScript objects, as well as special behavior. Massive "indexes" are actually property names, and they are strings. (According to the specification, implementations are free to optimize this, provided that things behave according to the specification.)

So, you get access to the property "0" in the array using a[0] or a["0"] ; a[0] is much more common and a little easier to type. :-)

Since you can use the property using parenthesis, a["length"] works just fine.

+2
source

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

Yes. You cannot do a.0 , though, since 0 not a valid Javascript identifier. (You can do a['0'] if you want.) In short, if you could not use the property name as the variable name, you cannot use dot notation to access the property.

In this case, ["length"] should also work (logically).

It works great.

+1
source

There are two ways to access JavaScript object properties.

  • Parenthesis designation
  • Dot notation

The parenthesis designation can be used for all properties, but dot notation cannot be used for numbers or strings with spaces and other types of characters that are not simple enough. Therefore, a.0 not legal, but a[0] in order.

The rules for using dot notation are given on the ECMAScript Specification page. You will notice that dot notation can only be used if the property name satisfies the syntax condition of IdentifierName.

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

 a['length'] 

and

 a.length 

OK.

+1
source

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

0
source

0 is technically a property of trees. It also means trees.0 will work. However, JavaScript identifiers must begin 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 entire statement invalid.

Length

also a property of trees. This is a valid identifier, so trees.length obviously works, but also another designation trees["length"] .

0
source

All Articles