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

154
Visualizações
How to get length of updated array using property in JS?

I created a variable to hold the total number of elements in the array using myArray.length. Then I use myArray.push() to add an element to the array.

the original array has 3 elements, so the .length returns 3, which is correct. But when I added an element to the array and I console.log(the Var which contains the .length property ), it just does not work. the Var still shows 3 for the array. However, if I use xxx.length, the number is correct now.

var collection = function() {
  //two properties
  var items = ["basketball", "football", "soccer"];
  var number = items.length;

  console.log(items);
  console.log("the number of the items in the array is " + number);

  // two private methods
  var addItem = function(newItem) {
    items.push(newItem);
    console.log(items);
  };

  var showNumber = function() {
    console.log(number) //this one returns 3, which is not correct
    console.log("the number of the items in the array is " + items.length);
  }; // this one returns 4, which is correct



  // two public methods
  return {
    getItem: addItem,
    getNumber: showNumber,
  };
}();


collection.getItem("pingpong");
collection.getNumber();

about 4 years ago · Juan Pablo Isaza
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