Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda