# ES6: var vs let

**URL:** <https://forum.freecodecamp.org/t/es6-var-vs-let/86963>\
**Category:** General\
**Created:** [February 13, 2017, 8:59pm UTC](https://forum.freecodecamp.org/t/es6-var-vs-let/86963 "2017-02-13T20:59:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbdiViklas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/abdiviklas/32/95315_2.png) [@AbdiViklas](https://forum.freecodecamp.org/u/AbdiViklas)\
**Post date:** [February 13, 2017, 8:59pm UTC](https://forum.freecodecamp.org/t/es6-var-vs-let/86963/1 "2017-02-13T20:59:16Z")

</div>

ES6 question here. So my takeaway from learning about `let` was "`let` is better than `var`, because hoisting. So stop writing `var` today and never use it again." This sounds like less than the full picture to me. So if so, are there times to use `var` and times to use `let`?

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [February 13, 2017, 9:21pm UTC](https://forum.freecodecamp.org/t/es6-var-vs-let/86963/2 "2017-02-13T21:21:22Z")

</div>

> [@AbdiViklas](#):
>
> So if so, are there times to use var and times to use let?

Nope! `var` really is on the way out. I would say that you should default to `const` rather than `let` and only use `let` if the variable’s value will change at some point.

---

<div class="post-metadata">

**Author:** ![rangelke](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@rangelke](https://forum.freecodecamp.org/u/rangelke)\
**Post date:** [February 13, 2017, 9:27pm UTC](https://forum.freecodecamp.org/t/es6-var-vs-let/86963/3 "2017-02-13T21:27:48Z")

</div>

Example from a blogpost [http://www.thinksincode.com/2016/10/16/its-ok-to-use-var.html:](http://www.thinksincode.com/2016/10/16/its-ok-to-use-var.html:)  
I however do think it’s not ok to use var.

function foo(bar) {  
console.log(baz); // undefined  
if (bar) {  
var baz = ‘Bar is truthy!’;  
}  
console.log(baz); // ‘Bar is truthy!’ (if bar is truthy)  
}

The second console.log would be undefined too, when using let or const. Then again, I think it looks nicer to define the variable at the top of the function in stead of relying on var hoisting.

---

<div class="post-metadata">

**Author:** ![nrandecker](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nrandecker/32/55725_2.png) [@nrandecker](https://forum.freecodecamp.org/u/nrandecker)\
**Post date:** [February 13, 2017, 10:36pm UTC](https://forum.freecodecamp.org/t/es6-var-vs-let/86963/4 "2017-02-13T22:36:17Z")

</div>

In my opinion you should be using let if you’re using ES6.

The main advantage is that let is scoped to its nearest enclosing block. [Here](http://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable) is a stack overflow article thats pretty good.

---

<div class="post-metadata">

**Author:** ![ayoisaiah](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ayoisaiah/32/9193_2.png) [@ayoisaiah](https://forum.freecodecamp.org/u/ayoisaiah)\
**Post date:** [February 14, 2017, 8:54am UTC](https://forum.freecodecamp.org/t/es6-var-vs-let/86963/5 "2017-02-14T08:54:29Z")

</div>

I don’t think there is any reason to continue using `var` over `let` and `const` except if you support older browsers that do not understand the new ES6 keywords. Even then, you can write ES6 code and transpile to ES5 using BABEL.

I agree with @PortableStick; default to `const` and only use `let` if you’re likely to reassign the variable at some point.

---

<div class="post-metadata">

**Author:** ![dariuszsobkowicz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dariuszsobkowicz/32/71522_2.png) [@dariuszsobkowicz](https://forum.freecodecamp.org/u/dariuszsobkowicz)\
**Post date:** [March 24, 2018, 9:57am UTC](https://forum.freecodecamp.org/t/es6-var-vs-let/86963/6 "2018-03-24T09:57:36Z")

</div>

totally agree with @PortableStick to use `const` as default, use `var` if you think in some cases will be useful

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 2:47pm UTC](https://forum.freecodecamp.org/t/es6-var-vs-let/86963/7 "2021-01-16T14:47:32Z")

</div>


