What's the difference with var, const, and let?

Hi so I’ve recently started taking JavaScript. I’m curious and slightly frustrated why there are 3 different ways to declare variables? Why not just always use var? or just always use const? This is something that has always confused me, could you please clear this up for me? :slight_smile:

Hey,

So, basically :

Var, const and let are all keywords you can use to define a variable. So , for example :

var Myname = Simon;

or

let Myname = Simon;

or

const Myname = Simon;

All define the variable we created “Myname” to Simon. But what is the difference between them ?

Var keyword is the old keyword used , from a different version of Javascript before ES6. We dont really use it anymore those days but its usefull to know what it does if you come across it.

Let keyword creates the variable and store the value ( Simon ) , until you change it to something else. Gives you the chance to change to stored value.

Const keyword is used to create a variable when you want to store the value in the variable and keep it forever. Basically you lock the content of the variable. If you try to change the variable assigned with the const keyword you will probably get an error.

4 Likes

I don’t really specialize in JS, but what I do know is that each has it’s individual uses.

var -I think was an old keyword used before ES6, but since let came out people have been using that.
let -Creates the variable and stores a value inside of it
const - Creates the variable, stores a value inside, but the value cannot be changed
Example:

const name='Jada'
let array1=['Ricardo', 'Mack', 'Gracie', 'Ruth']
let array2=[]
for (words of array1){
  if (words.length==name.length){
    array2.push('True')
  }
}
if (array2.length > 3){
  console.log('We have enough names!')
}else{
  console.log('We need more names!')
}

In the completely nonsensical program I hastily put together above, we can see that our entire program is dependent upon the length of name, which is why I declared it with const, so the value cannot be altered (well, const is funny about reassignment but anywho), for if it is altered we risk changing the purpose of our program.

This was just an example of how you can use the different reassignment keywords. There will be situations dependent upon your usage of those keywords and all I can really say is that each has it’s purpose, and you will find that

will be illogical in future projects.

2 Likes

As well as what @izzypt and @h1lo said, the major difference between var and let (apart from when they were released), is the scope. Variables declared with var are function or globally scoped, meaning if you define a variable with var outside any function, if/else, etc, the variable will be accessible anywhere:

var someVar = "hi";
function someFunc() {
  console.log(someVar);
}
someFunc();
//out>>"hi"

I can also do this:

if (condition) {
var x = "hi";
}
console.log(x)//out>>hi

let (and const), however, are block scoped, which means that they can only be accessed/changed by anything in the same block, or nested blocks.

What is a block?

A block is anything between curly braces:

function some() {
//all code here is block scoped
}

if (condition) {
//all code here is in a block
}

for (let i = 0; i > -3; i--){
//all code here is in a block
}

When things are in a nested block, it is a block within a block:

if (condition1){
  if (condition2){
      //this is a nested block
    }
}

This means I can do this:

function someFunc() {
 let variable = "no";
 if (true) {
  console.log(variable)//out when function called>>no
}
}

But I can’t do this:

function someFunc() {
if (condition1){
let x = "z";
}
if(condition2) {
console.log(z);//throws a "x is undefined" error
}
}

Here is some more reading on the subject. Scroll down to the var VS let VS const section for a quicker read.

I hope this helps!

2 Likes

Ohh! Thanks so much!!!

1 Like

That would be to declare a variable. It gets defined when we assign it something.

     let a;

     console.log(a)    //  undefined

We do not store anything in a variable. We assign it, whereby the variable becomes a reference to wherever that value is stored. Values have a place in memory. Variables point to them just like a business card. The business is not stored on the card, but its address is.

4 Likes

Thanks , good to know

1 Like