Home > OS >  Var and Hoisting
Var and Hoisting

Time:11-25

  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();
    

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

about question 1, I don't see any code. about question 2, you define twice the same variable and in the console, it will show it twice. because it is var, you will not get an error.

  • Related