javascript - Why array cannot be accessed like given below? -
below code returns true. if case why 4th line in code error out?
var x = ['a', 'e', 'f']; x[2]; alert('2' in x); alert(x.2);
the property names can access dot syntax conform javascript's rules identifier names (first character letter, _, or $, , remaining characters letters, numbers, _, or $).
what have there syntax error, , that's why errors out.
from mdn:
dot notation
= object.property;
object.property = set;
property must valid javascript identifier, i.e. sequence of alphanumerical characters, including underscore ("_") , dollar sign ("$"), cannot start number. example, object.$1 valid, while object.1 not.
you can use square bracket notation access property name, either of following return item want:
x[2]; x["2"];
Comments
Post a Comment