Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!