Scoping in JavaScript – Var vs Let keyword

As a seasoned JavaScript developer, you might already be aware of nuances of var keyword. JavaScript has been a loose language unless used in strict mode.

  • You don’t need to declare a variable before using it, unlike other programming languages that provide better compile time support and syntactic sugar. Variables that are not declared are hoisted to global scope.
  • JavaScript Hoists the locally declared variable using var keyword inside a method i.e. no matter where you declare a variable inside your method its always hoisted to top of method and available to all code branches within the method.
  • You can declare global functions as well using var keyword by defining them outside a method.
  • Since var creates a function scoped variable it can cause unexpected results with loops.

Continue reading