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

171
Visualizações
Adding function parameters creates variable shadowing, while no parameters result in reassignment?

Below I have 2 examples, with the only difference being that one function has a parameter while the other doesn't. But Example 1 will log hello to the console, while Example 2 logs hello! to the console.

My question is why does Example 1 log hello while Example 2 logs hello!.

Example 1, I'm uncertain of whats going on. My guess is that in Example 1 we are using variable shadowing. The greet parameter is is initialized to a primitive string hello. The greet parameter are shadowing the greet from line 1. Therefore when we log greet to the console we are logging the greet from the global scope, hello.

I'm not positive but, in Example 2, the function salutation is invoked with the argument 'hello'. Then the global variable greet is reassigned to the value of 'hello' + '!'. Therefore when greet is logged to the console, we are logging the value that greet was reassigned to, hello!.

Why does the lack of a greet parameter in the function allow us to log the reassigned value to the console. But when the function has a parameter we log the global variable to the console?

Example 1.

let greet = 'hello';

function salutation(greet) {
    greet = greet + '!';
}

salutation(greet);
console.log(greet); //hello

Example 2.

let greet = 'hello';

function salutation() {
    greet = greet + '!';
}

salutation(greet);
console.log(greet); //'hello!'

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The argument to the function is copied by value, so the greet (global) and the greet (local) are completely different. With the below line greet is a copy of the global variable and is only available inside the function, hence local.

function salutation(greet) {
    greet = greet + '!';
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Why does the lack of a greet parameter in the function allow us to log the reassigned value to the console. But when the function has a parameter we log the global variable to the console?

Both examples do log the global variable to the console.

It's just that in the first snippet, there is a local greet variable that gets reassigned, while in the second snippet there is no local variable and it assigns (and accesses) the global variable greet through closure.

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