Var VS let VS const

After successful understanding of variables in javascript. We can now move ahead to make a step ahead in learning basics of javascript. We can declare javascript variables in three different ways.

What are javascript three different variables?

  • Var
  • Let
  • Const

All these three ways of declaring javascript variables is same but they have different functionalities and working. We will discus all of this in this blog.

What is Var in javascript?

The first way to understand the variables is Var. Var is the simplest way to declare the variables. One of the most interesting fact in javascript is that you do not need to end every line of code with Semicolon(;) and one more interesting fact is you do not need to declare the variable with its data type like int, float or String etc. You just write down the name of variable and javascript compilation on runtime interprets it self whether the declared is an integer or string or float or a character.

Javascript assumes every variable as a Var if the variable is not decalred as const or let or var.

How do we use Var in Javascript?

Var is used in either local variables or global variables. We will understand it with a simple example. We assume that we have slightly understood the basic concept of Var. Let’s now jump in to the code to make our concept clearer.

Code:

var x=”I live in Pakistan”

console.log(x)  // Printing variable x

var x=”I love javascript” //over-writing variable x. You can over-write even without writing var

console.log(x) // printing to see the result

var x=”I live in Pakistan”

In above example variable x was declared which javascript identified itself as a string.

Console.log(x)  // Printing variable x

Print x to see the result.

Var x=”I love javascript” //over-writing variable x

Over-writing the variable x which was previously as “I love Pakistan”. There will be no error and it will be over-written. Thus, we came to the point that declaring variable with var can be over-written. Thus, during practical development the variable or a value that has to be updated time to time is used and declared with Var.

What is Let in javascript?

‘Both var and let work almost same as the var does work but the difference is a very slight but that creates a major distinguish between let and var.

Unlike var you can not declare the variable multiple times. For example In above example variable x was declared at the top as var x. later,  the variable x was again used  with var declaration. No error was shown and result was shown on the screen but when you are working with let you can not update it the way you had the privilege in using var. For example

let x= “Hello world”

let x=” Welcome ”

console.log(x)//Print on console

In above example I tried to use let with the same variable name to over-write it will throw an error “Re-declaring the variable already used“. So, you can’t do it in this manner.

But hey?

Isn’t there any other way to complete it or execute it?

Yes, here it is.

Correct Code

let x= “hello world”

   x=”Welcome”

console.log(x)

now, We did the same work as we did in var but with a slightly different way. So, From this point it has been a clear understanding that var and let are almost same but just a slight difference.

What is Cont in javascript?

Unlike var and let, Const works in a very different manner and way. All such values that you do not want to change through out your development and usage of the product you declare it as const. For example value of pi is 3.14 and is never gonna be changed so I will declare it as constant if I wished to use it in my program so it can’t be changed or updated.

For an instance we are going to understand it with the help of coding stuff and hope it would then be more understandable.

Code:

const x=10;

x=15; //Trying to update const value

console.log(x)

We won’t be able to change the value of x as it is now declared as const. If we change the const to let or var we would no longer than get error but our aim will not be justified then because we do not want to update the values used as const.

I hope you’d have got the very clear idea of let const and var. Please comment down the question below if you still find it difficult.

Buy on amazon with upto 40% off

RECOMMENDED FOR YOU

What is javascript?

Variables in javascript

Leave a Reply

Your email address will not be published. Required fields are marked *