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

128
Visualizações
Var and Hoisting
  1. My First Question: javascript takes declarations and put on the top of function code because of hoisting before any code executed, i want to know that in this situation which i stated below there is two variables declared they are written like this var a, a;? because of hoisting on the top of function code ? if like that why i am not accessing second variable a before initilization why i am accessing first variable a after initilization, In short on line 4 of function code console.log(a) // 10 how javascript decide to give 10 and not undefined, Note that there is two variables declare with same name on top.

  2. My Second Question: it is possible when javascript scan the code and find 2 variables with same name and it remove one variable and used another in the code execution.

    function myFunc() {
      console.log(a);
      var a = 10;
      console.log(a);
      var a = 20;
      console.log(a);
    
    }
    
    myFunc();
    
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Question 1: "Are two variables declared when written like this:

 var a, a;

Answer 1: No.

JavaScript source code is scanned before execution and in sloppy mode, any number of declarations of the same identifier ('a' in this case) as a variable using the var keyword are amalgamated and hoisted as a single variable declaration at the top of the function (or script element in the case of top level variables in a script file or inline element).

Named functions declared with the function keyword are also hoisted to the top of an enclosing function or script element with some differences:

  • Hoisted functions are compiled when hoisted and can be called before they appear in source.
  • Declaring a function with the same name as a previous function declared in the same scope replaces the previous declaration. Only code for the last same-named function in the same scope will be hoisted.

Question 2 Is it possible that when the Javascript engine scans the code and find 2 variables with same name, it removes one variable and uses the other in the code execution?

Answer 2: Yes

To restate it slightly, the JavaScript engine does not find two variables, it finds two variable declarations which when hoisted only result in a single binding of the variable name to where it is stored in memory - meaning only one variable is created.

about 4 years ago · Juan Pablo Isaza Relatório

0

  • var declarations are hoisted to the top of the function.
  • Duplicate declarations have no effect.
  • var declarations are handled separately from assignments.

You code is essentially the same as:

function myFunc() {
  var a; // Declaration is hoisted
  console.log(a);
  a = 10; // Assignment is not
  console.log(a);
  a = 20; // Second assignment is also not
  console.log(a);
}

var is distinctly unintuitive which is why modern code avoids it in favour of const and, occasionally, let.

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