Byeonghun Lee
Byeonghun Lee

Reputation: 13

Does the top of the function declaration and variable declaration help improve performance?

I usually use JavaScript. I declare functions and variables at the top when writing code. Because there is a hoisting in JavaScript.

I saw this style in many javascript books. One day I was curious. Is this style help Javascript performance?

Does declaring functions and variables at the top help improve JavaScript performance? Is this just style guide for human?

Upvotes: 1

Views: 95

Answers (1)

Abhishek Mani
Abhishek Mani

Reputation: 522

It increases the code readability as said above, since you know the ES6 has introduced let and const, where things are block scoped which is again a good practice to use while coding in Javascript, and also more convenient

Using var

 function nodeSimplified(){
  var a =10;
  console.log(a);  // output 10
  if(true){
    var a=20;
    console.log(a); // output 20
  }
  console.log(a);  // output 20
}

Using let

  function nodeSimplified(){
   let a =10;
   console.log(a);  // output 10
   if(true){
     let a=20;
     console.log(a); // output 20
   }
  console.log(a);  // output 10
}

Upvotes: 2

Related Questions