# Const vs Var Keyword

**URL:** <https://forum.freecodecamp.org/t/const-vs-var-keyword/363361>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [March 25, 2020, 3:10pm UTC](https://forum.freecodecamp.org/t/const-vs-var-keyword/363361 "2020-03-25T15:10:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ishaan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ishaan/32/134566_2.png) [@Ishaan](https://forum.freecodecamp.org/u/Ishaan)\
**Post date:** [March 25, 2020, 3:10pm UTC](https://forum.freecodecamp.org/t/const-vs-var-keyword/363361/1 "2020-03-25T15:10:28Z")

</div>

**Tell us what’s happening:**

The explanatory example uses const keyword to declare the array:  
_const countArray = countup(n - 1);_

In my solution, I have used var keyword which produces the same result. Is there any reason why const should be used instead of var.

**Your code so far**

```js

// Only change code below this line
function countdown(n){
  
if(n<=0)
return [];

else 
{
var arr = countdown(n-1);
 arr.unshift(n);
 return arr;
}
}
// Only change code above this line
console.log(countdown(5)); // [5, 4, 3, 2, 1]

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0`.

**Challenge:** Use Recursion to Create a Countdown

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [March 25, 2020, 3:18pm UTC](https://forum.freecodecamp.org/t/const-vs-var-keyword/363361/2 "2020-03-25T15:18:26Z")

</div>

When you have a variable that _shouldn’t_ be reassigned (any object, for example), it’s best to use `const` because then you can’t reassign it. If you have a variable that will be reassigned (strings, for example) it is almost always best to use `let`.

[https://evertpot.com/javascript-let-const](https://evertpot.com/javascript-let-const)

> **[Let’s use const! Here’s why.](https://ponyfoo.com/articles/var-let-const)**
>
> When reading a piece of code, others can take cues from these signals in order to better understand what we did. ES6 offers let and const as new flavors of variable declaration, are they better signals than var?

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [March 25, 2020, 3:28pm UTC](https://forum.freecodecamp.org/t/const-vs-var-keyword/363361/3 "2020-03-25T15:28:01Z")

</div>

Short answer, just use `const` and `let` instead of `var`.

For the code you have. If you use `var` the variable will be function scoped, if you use `const` it will be block-scoped, and as it is only used inside the else block it might as well be scoped to it. You also can’t reassign it.

It won’t really change much for the code, but it might help if you were to later do some code refactoring and by accident did something with the variable you didn’t really mean to (like reassign it or change it outside the else block).

A bit more on let, const, and scope.

> **[ES6 In Depth: let and const – Mozilla Hacks - the Web developer blog](https://hacks.mozilla.org/2015/07/es6-in-depth-let-and-const/)**
>
> ES6 In Depth is a series on new features being added to the JavaScript programming language in the 6th Edition of the ECMAScript standard, ES6 for short. The feature I’d ...

  
[You Don’t Know JS: Scope & Closures](https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/scope%20%26%20closures/ch3.md)

---

<div class="post-metadata">

**Author:** ![chuckadams](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chuckadams/32/109651_2.png) [@chuckadams](https://forum.freecodecamp.org/u/chuckadams)\
**Post date:** [March 25, 2020, 3:28pm UTC](https://forum.freecodecamp.org/t/const-vs-var-keyword/363361/4 "2020-03-25T15:28:25Z")

</div>

const and let also have more sane scoping rules. You can’t shadow a `var`, so you have to be careful about what you name your vars in any inner block, whereas you don’t need to worry about it so much with const/let unless you’re deliberately capturing outer scope. You also can’t use a const/let before it’s assigned, whereas a var will simply be `undefined`.

---

<div class="post-metadata">

**Author:** ![jaswindersbm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jaswindersbm/32/138839_2.png) [@jaswindersbm](https://forum.freecodecamp.org/u/jaswindersbm)\
**Post date:** [March 25, 2020, 3:41pm UTC](https://forum.freecodecamp.org/t/const-vs-var-keyword/363361/5 "2020-03-25T15:41:16Z")

</div>

Don’t worry about it now, this concept of `const` is explained very well in the next module i.e. ES6.

---

<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:** [June 1, 2021, 1:00pm UTC](https://forum.freecodecamp.org/t/const-vs-var-keyword/363361/6 "2021-06-01T13:00:32Z")

</div>

Closed on June 1, 2021, 1:00pm UTC


