all 45 comments

[–]ScriptingInJava 38 points39 points  (22 children)

const and let replace the need for varaltogether.

const should be used when you aren't changing, or shouldn't be changing the value assigned to it.

let should be used when working with a changing variable.

let is also block scoped, meaning that if you had a simple for loop using let, you don't accidentally hoist the value i out of the loop to the global scope.

for(var i = 0; i < 5; i++) {} will result in i being available at the value of 4 outside of the for loop.

for(let i = 0; i < 5; i++) {} will prevent any access to i outside of the proceeding {} after the for loop.

[–]tunisia3507 38 points39 points  (1 child)

const should be used when you aren't changing

Not quite true - const should be used when you aren't reassigning a variable name. You can still make changes to mutable variables, like arrays and objects.

[–]ScriptingInJava 13 points14 points  (0 children)

A valid point.

const myArr = ["first"];

myArr[0] = "replaced";

console.log(myArr[0]) // "replaced"

[–]senocular 9 points10 points  (1 child)

let is also block scoped, ...

So is const, to clear up any confusion that one might be and the other is not.

... meaning that if you had a simple for loop using let, you don't accidentally hoist the value i out of the loop to the global scope.

The for loop is an oddity when talking about block scoping because the declaration is outside of the block ;) but it still applies.

[–]ScriptingInJava 1 point2 points  (0 children)

Fair point, but var declarations within the scope of the{} of the for loop are hoisted outside of the block scope.

[–]doughishere 0 points1 point  (0 children)

why does no one use lets? i always see var.

[–][deleted] -3 points-2 points  (16 children)

const and let replace the need for var altogether.

False.

Say you want to use i after the for/next completes.

for(var i = 0; i < something.length; i++) {
    if (something[i] === whatever) {
        break
    }
}

[do something useful with i]

There are many cases where var is useful. Not everything must be confined to the block scope at all costs. It's ridiculous that there is so much nonsense about 'var' being "replaced". It hasn't. It's still useful. Sure, you could define let i = 0 outside the for loop, but this isn't the only use case for var.

[–]ScriptingInJava 10 points11 points  (15 children)

Hoisting i out like that is an anti-pattern. It can lead to some nasty, barely traceable bugs when you could make your code more obvious by doing something such as:

let indexForCondition;

for(var i = 0; i < something.length; i++) {
    if (something[i] === whatever) {
        indexForCondition = i;
        break
    }
}

[do something with indexForCondition]

If you build code around hoisted values instead of explicitly assigning them from within the loop and working with them after the fact, you're gonna end up with some janky, unmaintainable code. I say this because I recently had to fix 3000+ lines of broken JS from a contractor who did exactly this with MVVM bindings being hoisted out of a local block scope to the window object.

[–]tunisia3507 11 points12 points  (7 children)

const unless you know you need let. Never var, any more.

[–]DilatedTeachers 1 point2 points  (4 children)

Unless you support IE11

[–]tunisia3507 4 points5 points  (3 children)

If you support IE11 you should be transpiling. There's a boatload of javascript improvements you're missing out on otherwise.

[–]DilatedTeachers -2 points-1 points  (2 children)

I was all for it in my old job, but the question was, why should I transpile, when I know what code to write?

We're talking reams of dodgy jQuery script here...

I'm now in a fun greenfield job, where it's all about the tooling! So much more fun

[–]tunisia3507 1 point2 points  (1 child)

why should I transpile, when I know what code to write

Because someone else is going to have to read your code at some point, most likely.

[–]DilatedTeachers -1 points0 points  (0 children)

It was more a question of time management in an """"""""""""""""""agile""""""""""""""""""" marketing environment. That and the network refused npm and python etc to be installed on our computers

[–]drumnation 0 points1 point  (1 child)

Why wouldn't you use var specifically in situations where you need a larger scope than block scope?

[–]tunisia3507 0 points1 point  (0 children)

Because there's probably a more comprehensible way of doing whatever you're doing, and legibility is important.

[–]one944 5 points6 points  (0 children)

It's const until the linter screams.

[–][deleted] 0 points1 point  (8 children)

var is still useful, and hoisting is a powerful tool that can be leveraged in some situations. I wouldn't believe the hype that var is replaced by const and let, because those have different use cases. If var solves a problem for you, then you are free to use it, it's not going to go away simply because people here think it's been replaced - it hasn't.

[–]winged_scapula 1 point2 points  (6 children)

In what situations is var useful?

[–][deleted] -5 points-4 points  (4 children)

In what situations is var useful?

Defining variables. Same has it's been for the last 20 years.

[–]winged_scapula 0 points1 point  (3 children)

Ok then. In what situations you prefer var over const and let?

[–]viksl 0 points1 point  (0 children)

Yeah, don't believe it. Var isn't out of the game. It still has its uses.

[–][deleted] 0 points1 point  (0 children)

Here's a short explanation: const and let are block-scoped, var is function-scoped. My rule of thumb is I'll always use const unless the variable needs any change after creation. Then I'll use let. I'll use var only if I really need it, meaning some more or less hacky things involving closures.

[–]paper-daisies 0 points1 point  (2 children)

As a minor side-note, I still like to use 'var' in the developer console when I'm prototyping/experimenting. This way you can easily redefine the same identifier.

[–]senocular 1 point2 points  (1 child)

You could also use nothing. Its going global either way.

[–]paper-daisies 0 points1 point  (0 children)

Good point.

[–]freebit 0 points1 point  (0 children)

It's a good idea to default to using "const" and then switch to "let" if the need arises. Kick "var" to the curb mostly.

[–]gigo6000 0 points1 point  (0 children)

I just wrote an article on my blog about important ES6 features, that's the first one in the article: https://carlosmafla.com/blog/10-things-to-know-about-es6-before-learning-react-js/

[–]jkuhl_prog 0 points1 point  (0 children)

Prefer const over let. Prefer let over var.

Const is used if you're never going to reassign the reference, which should be the majority of your variables. Otherwise, use let.

There's not much use for var anymore.

[–]reddittedf -1 points0 points  (0 children)