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

371
Visualizações
why array length in JavaScript does not change after adding property?

why does myArray.length is 3 here? I have added a new property in it, to my guess, it should be 4.

    var myArray = [ "foo", 42, "bar" ];
    
    myArray.baz = "baz";
    
    myArray.length; // 3
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

  1. In the first line, you create a variable myArray and you add a value to it. Every variable in Javascript is an object, that has keys and properties (see more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)

Between [] you create an array type, that stores data and labels them with numbers. (see more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)

  1. In the second line, you add a baz property to myArray with "baz" value
  1. In the third line, you get the length property of the array

If you want to add a(n) "baz" element to the end of the array, you can use the .push() method. For example: myArray.push("baz");

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to use Array.prototype.push() to add new elements to array. In your code myArray.baz = "baz" doesn't do anything.

var myArray = [ "foo", 42, "bar" ];

// your approch
myArray.baz = "baz"; 

console.log(myArray.length); // 3
console.log(myArray) // [ "foo", 42, "bar" ]


// correct 
myArray.push("baz")

console.log(myArray.length); // 4
console.log(myArray) // [ "foo", 42, "bar", "baz" ]

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