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

184
Vistas
Assigning value to an array or object inside a function

I am learning javascript and I read that in javascript array and objects are by default passed as a reference. So when I do this:-

var a = [2, 3, 4]

function pushEl(a, num) {
  a.push(num)
}

pushEl(a, 5)

console.log(a)

The output is as expected which is

[2,3,4,5]

But what I am not able to understand is that when I assign value to an array or an object inside the function the original array/object is not changed.

var a = [2, 1, 3]

function change(a) {
  a = [1, 2]
}

change(a)

console.log(a)

I expect the output to be [1,2] but the output is [2,1,3].

If the array is passed by reference then the changes should have been reflected in the original array too.

Can anybody tell me what concept I'm missing here?

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