DEV Community


Discussion on: Stop using var for declaring variables !!!

peerreynders profile image
peerreynders • Edited

If that is an issue, an entirely different conversation is necessary: Why is this type of code running in the Global Scope?

var is constrained (does not leak) within functions and within modules - so nothing about ES2015 or ESNext will make code that uses var "break".

I don't want to see this kind of bug.

  • How far in your process is f(m) going to get undetected given that m is going to be undefined?
  • How long are your functions that you may lose track that you already used m?
  • How descriptive is m anyway - likely two different names are more intention revealing.

In the majority of cases let and const are better - but that doesn't mean all cases.

Thread Thread
larsonnn profile image
Lars Feldeisen

they whole problem if the hoisting of var. Block Scope is safer in your daily programming. Why to I need to care of hoisting.?

try {
  var something = createSomething(); // something is now available in the whole function / global
} finally {
  if (something) something.freeResources();
Enter fullscreen mode Exit fullscreen mode

You only need to free resources when you doing something you don't wan't anything to access it later. The garbage collector will do the rest for you. So this example is shit. If you know how to program without side effects there is no real case you would go into this.

var could causing side effects if you don't carefully watch that you don't need it in the function. It has no benefit except you call hoisting one. So you want hoisting. And in my opinion (because that's all here) its shit.

Thread Thread
bryonmapes profile image
Bryon Mapes

I'm not personally a fan. I would rather use block scope with let.