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

Var, Let, Const | Difference?

I know that var defines variables. Constant's define variables that can't change. But what the heck is let?

var banana = true;
const apple = true;
let orange = true;

No problem, joey happy coding :smile:

2 Answers

var is the old way to define a variable.

const is the variable is static and doesn't change. So if you try to change it later in program it will throw in error.

let is new way to define a variable within the same scope of brackets.

So you don't know js in this chapter talks about scope.

const blah = "lol"
blah = "mutate"

https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch3.md

let hey = "foo";

let show = function() {
  let hey = "print";
  console.log(hey)
}

show() // prints hey

another scope example

var hey = "foo";

function yo(){
  var hey = "yo"
  function blah () {
    console.log(hey) // undefined
  }
}

we have es2015 class you can learn more here

https://teamtreehouse.com/library/introducing-es2015

Thanks.

let declares a variable that is block scoped, meaning that if it is declared within a code block like say an if statement block, loop block or anything like that then the variable will only exist within that block.

var example = 5

if (example > 2) {
  let example2 = 100;
}

console.log(example2);

Take the code above for example, what do you thing will be printed out if you actually ran that code? You would get an error stating that example2 is not defined, this is because it only exists within the if statement's code block (inside the opening and ending bracket of the if statement) once you get outside of the block where it was declared it stops existing essentially.

If you ran the above example but changed let to var then the code would print 100, since var variables are function scoped, meaning that once declared they exist anywhere within the function that they are declared in.

In most languages block scoping is the default for all variables, JavaScript is actually somewhat unique in that variables used to be function scoped by default, and this is something that has often been criticized as being a fault of the language as it can often lead to mistakes and confusing code that is harder to follow than it should be.

Therefore let is meant to essentially be a superior replacement of var, the only reason why var still exists is for compatibility reasons. So if you are writing new code designed to run on ES2015 compatible browsers then you pretty much always use let over var.

Edit: Added some extra info about let.

Thanks.