What's wrong with 'var'?


#1

Whatever happened to var?

I did the last Javascript course Codecademy had, and their jQuery course.

I wanted to do this new course to get up-to-date on Javascript syntax.

Now, I’m hearing that var, one of the things that I used most, is not to be used! What’s up with that?

Now, I’m supposed to use const and let. Const seems like it could be helpful, but let? That just seems like a bad replacement for var. var and const actually stand for what they are, but let? It seems to stand for nothing!

Codecademy doesn’t say what it stands for.

Can somebody please tell me the problem with var? And also please tell me what let stands for.


#2

the new courses teaches es6, here is a good answer about var:

this becomes very clear when we start using loops:

for (let i = 0; i < 5; i++){
  console.log(i)
}
console.log(i)

and:

for (var i = 0; i < 5; i++){
  console.log(i)
}
console.log(i)

in the first code, using let, i only exist inside the loop (block scope, only exist within the loop block) where as var (second code example) has a function scope (it still exist after loop block, even though it would make sense that i only exist inside the loop (block)


#3

It does not have block scope. Therefore, it can lead to unpredictable behavior if you’re not careful (due to hoisting).

ES6 gave us 2 new ways to define variables, each of which has block scope. The key here is block scope.

What does block scope mean? Why is it important?

Look at the code that @stetim94 gave you above.

The first example uses let to define the variable i. The expected behavior is that you are declaring the i variable to act as an index or loop counter within the for loop only. Therefore, you’d want that variable to only work within that for code block, meaning within the for curly braces.

Does that make sense?

Why use let instead of var?

It makes our code more readable and predictable. We are declaring that this variable has this specific scope, i.e. where we defined it.

Think of it as let this variable represent this value. You use it to declare a variable with a given block scope.

When to Use let vs. const

Use let anytime you need to change what is assigned to it, as that changes it’s identifier in memory. Good examples are the for loop above, strings, etc. where what is assigned to it may change within that scope.

Use const when what’s assigned to it will not change. Don’t get confused here, as const is mutable. Good examples are objects, arrays, and function expressions assigned. What is within the object or array may change, but the identifier to a specific object instance or array will not change.


#4

But this has only changed since es6, right? It used to be that var also had blocked scope, like let does now.


#5

var never had block scope, even in es5 and earlier. It had and still has function scope

es6 introduces let and const which have block scope, and are often the better choice


#6

Thanks for your answer, but @stetim94 answered my questions.


#7

@hellofromtonya covers certain areas in more depth, some of documentation provided is absolutely worth the read


#8

Okay, I’ll read it, since you reccomend it.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.