Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assigning properties to array gives length 0 in javascript

We can assign properties to an array, but why the length in this case is 0?
See the code attached

var person = []
person.fname = "Mr. Brown"
person.lname = "White"
person.length // gives 0 why?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

See the specification:

The "length" property of an Array instance is a data property whose value is always numerically greater than the name of every configurable own property whose name is an array index.

and

An integer index is a String-valued property key that is a canonical numeric String (see 7.1.21) and whose numeric value is either +0𝔽 or a positive integral Number ≤ 𝔽(253 – 1). An array index is an integer index whose numeric value i is in the range +0𝔽 ≤ i < 𝔽(232 – 1).


Arrays are designed to hold a ordered sequence of values represented by integer properties (0, 1, 2, etc).

While they are objects and so you can shove any property you like on them, that isn’t what they are designed for.

The length is designed to count the ordered sequence of values. It isn’t a count of every property of any name.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading