Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Alita Edmonds
Alita Edmonds
6,068 Points

What is the difference between let, const, and var?

I am wondering what the difference is between let, const, and var. All of a sudden, all the Javascript courses I am watching are using these new variables. What is the difference? Thanks in advance!

4 Answers

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

Hey Alita Edmonds , it's a very good question . Let's have a look -
Var
The JavaScript variables statement is used to declare a variable and, optionally, we can initialize the value of that variable.

Example: var a =10;

Variable declarations are processed before the execution of the code. The scope of a JavaScript variable declared with var is its current execution context. The scope of a JavaScript variable declared outside the function is global.

Consider the code-

function nodeSimplified(){
  var a =10;
  console.log(a);  // output 10
  if(true){
   var a=20;
   console.log(a); // output 20
  }
  console.log(a);  // output 20
}

In the above code, you can find, when the variable is updated inside the if loop, that the value of variable "a" updated 20 globally, hence outside the if loop the value persists. It is similar to the Global variable present in other languages. But, be sure to use this functionality with great care because there is the possibility of overriding an existing value.

let
The let statement declares a local variable in a block scope. It is similar to var, in that we can optionally initialize the variable.

Example: let a =10;

The let statement allows you to create a variable with the scope limited to the block on which it is used. It is similar to the variable we declare in other languages like Java, .NET, etc.

Consider the code-

function nodeSimplified(){
  let a =10;
  console.log(a);  // output 10
  if(true){
   let a=20;
   console.log(a); // output 20
  }
  console.log(a);  // output 10
}

It is almost the same behavior we see in most language.
The scope will be well maintained with a let statement and when using an inner function the let statement makes your code clean and clear.

const
const statement values can be assigned once and they cannot be reassigned. The scope of const statement works similar to let statements.
Example: const a =10;
Question: What will happen when we try to reassign the const variable? Consider the following code-

function nodeSimplified(){
  const MY_VARIABLE =10;
  console.log(MY_VARIABLE);  //output 10
  MY_VARIABLE =20;           //throws type error
  console.log(MY_VARIABLE); 
}

Error Message : Uncaught TypeError: Assignment to constant variable. The code will throw an error when we try to reassign the existing const variable.

Hope this helps :)

Alita Edmonds
Alita Edmonds
6,068 Points

This cleared up everything! Thank you!

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

var is old and has issues with scoping. It has been upgraded to let and const. var is still around for backward compatibility.

Check out links like what Robbie posted above for the details.

Here's a var vs let codepen example

Alita Edmonds
Alita Edmonds
6,068 Points

Thank you! Will check that out!