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?
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?
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.
Array stores elements. If you want to call person.name= "Mr.Brown" You can actually implement it by:
//Initialize the variable
let person = [];
//Specify the object that will be pushed to the Array 'Person'
let objectToPush = {name: 'Mr. Brown'};
//Push the object
person.push(objectToPush);
//Get Length
console.log(person.length); //<--- Will return 1;
//Call the name property inside the object
console.log(person[0].name); //<-- returns 'Mr.Brown'