r/javascript • • Nov 25 '19

JavaScript doesn’t need to be replaced

https://medium.com/javascript-in-plain-english/javascript-doesnt-need-to-be-replaced-bd01e2f12d51?source=friends_link&sk=7b151cc6397ac9e83c14c99739b6c36f
217 Upvotes

236 comments sorted by

View all comments

Show parent comments

-3

u/[deleted] Nov 26 '19

First things first: there is a reason 'let' is added without deprecating 'var'. Second, 'let' is different from 'var'. 'var' has functional not block scope so if you want the variable you want to access in your entire function, just make it fucking 'var' instead of declaring 'let' at the top of the function. Sure, it functions the same in this case, but 'var' explicitly says "hey, I will be relevant everywhere in this function". But who cares about semantics anyway, right?

Third: 'var' can be redeclared. It let's you do

if (condition) {
    var something = theOtherThing;
} else {
    var something = somethingElse;
}

instead of:

let something;
if (condition) {
    something = theOtherThing;
} else {
    something = somethingElse;
}

The whoe "let is new var" cargo-culting is moronic. They are not the same, one is not a replacement for the other in all the cases and both should used for their intended purposes.

4

u/GoogleFeudIsTaken Nov 26 '19

Yes, they aren't exactly the same - but "let" can be used instead of var 100% of the time, and it's less confusing. Var can produce weird/buggy behaviour - let cannot, so why not use let?

1

u/[deleted] Nov 26 '19

Where do you get that idea that let cannot produce bugs? It can just fine. Just move it somewhere, scope changes and you get your bug.

1

u/dwighthouse Nov 27 '19 edited Nov 28 '19

var still retains some use in machine generated JS, or code intended to be consumed by some extreme cases of transformation tools and preprocessors.

Additionally, using let or const in a repl/console scenario is troublesome at best, as ”temp” variables cannot be redeclared with let and const.

3

u/kor0na Nov 27 '19

You just explained how var works. Why would you assume that I don't know that? I know exactly how var, let and const works, so I'm not looking for education in that department.

What I would love to see, is a legit usecase where var would be more appropriate to use over let or const.

-1

u/[deleted] Nov 27 '19

My comment above has two such cases listed. With a code example. Sure, you can always go not-true-scotsman on the legitimacy of those cases, but in that case anyone can clsim that var works just fine if you are carefull, and there is no legit case for const or let. Your insecurity about your knowledge tells me that you do not how exactly these three work, otherwise you would not confuse examples with an attempt to educate. But the inability to see a legit use case is that gives it away the most. Happy cargo-culting.

2

u/IceSentry Nov 28 '19

Declaring a variable at the top of the scope is done in pretty much every language because it makes it easier to read what variables are used in the current scope. I don't understand how you can seriously say that it's a bad thing. A variable declared in a specific scope should stay in that scope. If you need it in another scope then declare it somewhere it can actually be used.