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

153
Vistas
how does function has access to const before declaration?

I recently saw this questions:

function compareMembers(person1, person2 = person) {
  if (person1 !== person2) {
    console.log('Not the same!');
  } else {
    console.log('They are the same!');
  }
}

const person = { name: 'Lydia' };

compareMembers(person);

By seeing this, I am wondering how person can be the default value for person2.

const are not hoisted unlike var. And even if I try to do a console.log(person)

before function compareMembers I am getting Reference error.

IF any one can explain me how come the function has access to person as default value it would be really a great help.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your code will run like this:

  • Stored variables to Stack (compareMembers, person). And referent to Heap Memory
  • Stored object, function to Heap Memory (dynamic)
{ name: 'Lydia' }

and

function compareMembers(person1, person2 = person) {
  if (person1 !== person2) {
    console.log('Not the same!');
  } else {
    console.log('They are the same!');
  }
}

Then run Call Stack

  1. compareMembers(person)

You can read document of call stack and stack and heap memory

about 4 years ago · Juan Pablo Isaza Denunciar

0

JavaScript, as you know, is applied line by line In the first part, you came and created the function and did what you wanted inside

You take the first input from the user and the second input by default, if it does not work, you fill it with a variable

Now, in the next part, you created a variable, my friend, and you gave a function to this input, and this actually applies to both of your inputs, and this is absolutely true.

It only works in the opposite condition in your function if you give two inputs with different values

I hope I was able to help my friend

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