Question after the First Class

What is the difference between var and let statements?

Ramón said something about who the values change, but I did not get it.
For instance, the two lines below give the same outcome.
var a=1; a=++a; console.log(a); // outcome 2
let b=1; b=++b; console.log(b);// outcome 2

3 Likes

Thank you for the question! With the semi-colons, you can run multiple “lines” of JavaScript on a single line.

Hope this clarifies things!

5 Likes

let is the best practice variable declaration. It has built-in protections that will give you an error message if you try to declare a variable with the same name. For instance:
var myVariable = “Hello”;
var myVariable = “Howdy”;

var lets you declare a variable multiple times which can lead to problems in your code.

If you tried the above with let it would give you this:
let myVariable = “Hello”;
let myVariable = “Howdy”

And you would get the error message “SyntaxError: unknown: Identifier ‘myVariable’ has already been declared.”

6 Likes

their difference is that a variable declared in using var is declared in the global scope and variables declared with let are block scoped variable but these concepts are not discussed yet in the course , if you still want to know more about it fell free to send me an email at mbayelel@gmail.com

5 Likes

let is best use to define variables and var to declare them

you will learn better about it once we study the scope of a variable

4 Likes

It’s when you think you are declaring a variable for the first time.
var … = … or let … = …
If you go to declare the variable again for the ‘1st’ time, var will allow it, so you might end up with errors in your code. But let will tell you in the console that it’s already been declared.

3 Likes

The difference between var and let is that with var you can redefine the same variable. But with let you can’t. Below code is perfectly okay.

var a;
var a;   // it should run

But the code below this line isn’t.

let a;
let a;   // it should generate and error

And, yes obviously it will give same outcome as you have used prefix with both. If the two variables have same value and you are applying same operations on them the results will be same. I guess you will understand better if you relate it will algebra.
And if you are asking about the prefix, postfix part. Then, let’s use prefix first

var a = 1;   // variable a have value 1
a = ++a;   // here ++a is used which means the value is incremented in this step
console.log(a);   // the output should be 2 as the value has been incremented

But if you use postfix,

var a = 1;   // variable a have value 1
a = a++;   // here a++ is used which means the value will be incremented after this step
console.log(a);   // the output should be 2 as the value has been incremented

Now lets use both

var a = 1;   // a = 1
console.log(a++);   // a will be equal to a + 1 after this step, right now it should be 1
console.log(a);   // a is incremented, it should be 2
console.log(++a);   // a = a + 1, this action is performed in this step so a should be 3
console.log(a);   // a should be 3
4 Likes

To sum it all up, in modern JavaScript it’s best practice to use let & const keywords for variable declaratation.
var was used until ES5 providing global & function scope. In ES6 (2015) let & const was introduced (among other things) and they provide block scope.
Scope is something that is going to be discussed later on but if you want to check all about it from now have a look here:
Scope - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
and another nice article about var, let & const Var, Let, and Const – What's the Difference?
Hope that helps :slightly_smiling_face:
PS: Don’t worry, everything is gonna fall into place. It just takes some time and you’ re going to have alot of aha moments :grin:
(I’m still learning too)

5 Likes

Thanks a million! I have learnt a lot from all your answers.

1 Like

In terms of being a variable, a box to keep a value for later, there is no difference between var and let. The benefit of let is safety. With var I can declare a variable many times - this can lead to errors. I could give a variable a name I have used before which would kill the previous variable by mistake.
With let this cannot happen if I use a name which I have used before I will get an error.
Well, that is how I understand it.
Later, when we get to more complex concepts, they will tell us the other differences relating to the scope which I think is also different.

3 Likes

Hi Sunshine,

Not a direct answer to your question but some context as to why JS has three ways of declaring a variable (var, let, const).

Remember Ramon said the JS was created in a couple of weeks about 25 years age and “var” was in the language from the beginning. As JeffScript said in a previous post - “let” and "cons"t were not added in until ES6 was released in 2015. So why do we need three ways of doing the same thing?

Well a lot has changed since 1995. The HTML and CSS languages have evolved greatly since then, the web has expanded onto to devices that we could only dream of (phones, smart phones, tablets, watches) and the worldwide web itself has assumed a larger and more important role in all our lives - not just as an information sharing service but as a way of doing many of the basic things that we need to do to live our lives (shopping, telemedicine, registering to vote, banking, etc). As such writing robust, less buggy software in not just important it is vital! So the language has to keep up. As previous commenters have noted “let” will throw an error if you try to redeclare a variable (which, among other things, makes it more difficult to mess things up if more than one programmer is working on a project. A “const” can’t change its value and - again - will throw an error if you try to change it. There are times when this is incredibly useful (again particularly on larger projects).

So that explains why we have new keywords for declaring variables, but why keep “var” around - why not just get rid of it? Well, the keepers of the JS language standard are committed to extreme backwards compatibility - in fact the very first JS program ever written and released on the web will still run today in every modern browser. Think about it - if your online banking software suddenly stopped working because “var” was no longer recognized our lives (as users) would be chaos (as programmers we would probably have a lot more work - but going into old code and changing “var” to “let” or “const” would, for me anyway, be incredibly boring.)

The reverse problem does come up. People are forced for some reason to use older browsers that can not run modern javascript. There is a good article about it at: The Optional Chaining Operator, “Modern” Browsers, and My Mom - Jim Nielsen’s Blog and it’s worth the read.

So yeah, we have to learn about “var” (because we’re going to see it in legacy code) as well as “let” and “const” because that’s the way we write code today and no doubt we will have to learn (relearn) many more things, because just when you think you know something in a programming language…something new comes along.

Welcome that’s the world we live in (and I, for one, love it.)

5 Likes

can’t re-declare let but can in var

let is a block based variable , while var is not

1 Like