DEV Community

loading...

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

larsonnn profile image
Lars Feldeisen • Edited

Thanks for sharing. Im gonna work it through and look if this still the case. 9 years difference (according to the engineer discussion)

Thread Thread
mindninjax profile image
Rishabh Singh ⚡ Author • Edited

Hey Lars! I will be replying to this comment of yours:

What target is that? the "I just learned to walk"? Constants are a basic concept of programming. And to point out that a Javascript const is not the same as a C const is not to hard.

Also you're completely right and I don't know why var still exist.

Alright, so our targeted audience is mostly comprised of people who are in their early stages of the coding journey and are just getting started, if you check our profile you will find most of the tutorial belong to that category only.

One of the main reasons we prefer to limit our audience is so that we can match the requirements of a beginner and make sure our explanation is understandable to them.

I hope this answers your question!

Have a great day!

Thread Thread
peerreynders profile image
peerreynders • Edited

Also you're completely right and I don't know why var still exist.

2021-Feb-25:

use var

Context:

try {
  const something = createSomething();
} finally {
  // ugghhhh just let me access `something` here
  something.freeResources();
}
Enter fullscreen mode Exit fullscreen mode

doesn't work because of the block scope const something is constrained to. var is scoped to the execution context so

try {
  var something = createSomething();
} finally {
  if (something) something.freeResources();
}
Enter fullscreen mode Exit fullscreen mode

will work. Otherwise one needs to resort to

let something;
try {
  something = createSomething();
} finally {
  if (something) something.freeResources();
}
Enter fullscreen mode Exit fullscreen mode

Just because you don't use it doesn't mean that it isn't useful.

Thread Thread
patarapolw profile image
Pacharapol Withayasakpunt • Edited

var add no real benefit over let in the case, and will break if I cover everything with ES6 block scope

Thread Thread
peerreynders profile image
peerreynders

Or is it that let doesn't add any real benefit in this case - and it is noisier as it requires a separate line of code just for the declaration.
And I don't understand what you mean with "will break if I cover everything with ES6 block scope".

Thread Thread
patarapolw profile image
Pacharapol Withayasakpunt • Edited

var can be scoped with IIFE, unless you expect scope leak.

let and const can be scoped with just {} alone.

One line of code doesn't matter that much (unless TypeScript - you need to and let type declaration).

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

fn(m)

// ... 10+ lines of code

try {
  var m = throwableFn()
} catch (e) {
  m.cleanup()
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
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.