Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda