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

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -