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

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

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 Denunciar

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 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