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

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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