Why is the curriculum still using var in the beginning?

This has probably been asked alot, why start the curriculum using var instead of let???

This gets asked every so often and there are lots of good discussions about this if you search the past posts.

Short answer:

  1. var is still a part of JS and learners need to be aware of it.

  2. The curriculum is built and maintained in no small part by volunteers. We can use our limited developer resources in better ways than eradicating every single mention of var in the curriculum.

var should not be the first thing introduced to new learners imo, a better route is “hey this exists, but it is not a best practice, use let”

I am sure there is someone who can volunteer to take this on to fix it, this would give them some experience in web development

1 Like

As someone who is brand new, could you explain this to me, please?

If you have the time, please go ahead and make an issue on GitHub and/or start a PR. It’s a bit bold to make statements about how much time other volunteers have.

Could you link me please so I can understand this please?

Basically, there are three ways to declare a variable in JavaScript, let, const, and var. Using let and const is a best practice, but var is still in the language for backwards compatibility.

As a general rule

  1. Use const if you can. This is for variables that will not change the value they hold.

  2. Use let if you can’t use const. This is for variables where you need to change the value they hold.

  3. Don’t use var unless you are maintaining older code that does not support const and let.

Is it safe to use let in challenges?

Respectful disagree. E.g. var has function scope whereas let has block scope; var variables are hoisted whereas let variables aren’t. They behave differently and their use is situational.

Are these things I should research and try to understand at the beginning, or something that will make sense in time?

Sure, it’s safe to use let. It’s ‘safe’ to use all three. But const and let are preferred (in that order). Const is preferred to let so you don’t accidentally change something that should not have been changed. Let and const are both preferred to var due to easier understanding of the scope. Const and let are defined inside of the nearest {}s while var is more complex.

1 Like

No, using const and let is a best practice. Using var is discouraged in new code. The scoping differences are exactly why let and const are recommended over var.

For the beginning exercises, it won’t be a big difference for you. Let and const will be introduced in the curriculum.

So to make sure I am understanding and can put this in words that make sense to me.
Var - Global and can be defined in multiple places, has very complicated interactions because of this potentially.

Const- Defines a value for the nearest {} and the value is immutable must be redefined to be used in a later function

Let - defines a value for the nearest {} that is mutable within that function, then must be redefined afterwards for reuse

Are these correct?

Var isn’t automatically global. In my opinion, it’s not worth trying to sort out the ‘hoisting’ rules for var unless you find a situation where you really need to know down the line.

For let and const, mutable vs unmutable is tricky because the behavior depends upon if your variable is holding a primitive value or a complex data type, such as an array or object.

With let your variable is fully mutable.

With const primitive values (like numbers or boolean values) can’t be changed. But with arrays or objects, which array or object the variable refers to cannot change but the contents of the array or object can be changed.

So with const, when I declared an array and said the array had X number of elements and each Element had Y number of spaces inside of it, I could change the values in the Y spaces, but not the number of Y spaces or the number of X elements in the array?

Not exactly.

const myArr = [1, 2, 3];
// This is ok
myArr.push(4);
// Also ok
myArr[0] = 100;
// This is not ok
myArr = [5, 6, 7]; // you'll get a syntax error

You are free to change what is in this array or how many elements it has, but you can’t change which array your variable is referring to.

So the thing is the thing, and in this case when using it in this way this is the only thing that can be referenced?

Basically, yeah. There is a more complicated reason under the covers as to why if you are curious, that ties back to lower level languages.

  • A variable is a reference to a value.

  • When you initialize (assign a value to) a variable, you are establishing a referential link between the variable and the value. The innards of the value, and whether they change or stay the same, are matters separate from the referential link.

  • A variable declared with var or let can be assigned a new value, meaning breaking the existing referential link and establishing a new one.

  • A variable declared with const has it’s referential link forever in place. The link can’t be broken, meaning you can’t assign a new value to the variable.