Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
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?
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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'
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!