# When do you need "var"? Can you only use "let" to declare variables?

**URL:** <https://discourse.processing.org/t/when-do-you-need-var-can-you-only-use-let-to-declare-variables/19673>\
**Category:** Beginners\
**Created:** [April 11, 2020, 7:15pm UTC](https://discourse.processing.org/t/when-do-you-need-var-can-you-only-use-let-to-declare-variables/19673 "2020-04-11T19:15:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mnoble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnoble/32/99_2.png) [@mnoble](https://discourse.processing.org/u/mnoble)\
**Post date:** [April 11, 2020, 7:15pm UTC](https://discourse.processing.org/t/when-do-you-need-var-can-you-only-use-let-to-declare-variables/19673/1 "2020-04-11T19:15:25Z")

</div>

Hello Forum,

Can p5js variables be declared and used exclusively with “let” or are there instances when you need “var”?

Thank you for your help

```auto
let move = 150;
let on = true;  

//or

var move;
var on;   

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 11, 2020, 8:30pm UTC](https://discourse.processing.org/t/when-do-you-need-var-can-you-only-use-let-to-declare-variables/19673/2 "2020-04-11T20:30:28Z")

</div>

Hello,

There is a video here discussing this topic:

[![](https://img.youtube.com/vi/q8SHaDQdul0/maxresdefault.jpg "16.1: let vs var - Topics of JavaScript/ES6") ](https://www.youtube.com/watch?v=q8SHaDQdul0)

`:)`

---

<div class="post-metadata">

**Author:** ![mnoble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnoble/32/99_2.png) [@mnoble](https://discourse.processing.org/u/mnoble)\
**Post date:** [April 11, 2020, 8:54pm UTC](https://discourse.processing.org/t/when-do-you-need-var-can-you-only-use-let-to-declare-variables/19673/3 "2020-04-11T20:54:13Z")

</div>

Very helpful. ‘Let’ it is!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 11, 2020, 10:08pm UTC](https://discourse.processing.org/t/when-do-you-need-var-can-you-only-use-let-to-declare-variables/19673/4 "2020-04-11T22:08:34Z")

</div>

> [@mnoble](#):
>
> … or are there instances when you need `var`?

There are very few edge cases for `var`.

I guess the most important 1 is when we need to create some global namespace object across multiple JS files like in the example below:

https://glitch.com/embed/#!/embed/radio-pick-active-sketch?path=sketches/0.js&previewSize=0&sidebarCollapsed=false

If we had declared variable _sketches_ w/ `let` in place of `var`, the code above would crash, b/c once a variable is declared w/ `let` it can’t be redeclared anymore within the same scope.

BtW, JS got 6 keywords for declaring variables:

1. `var`
2. `function`
3. `let`
4. `const`
5. `class`
6. `import`

Among those 6, only the keywords `var` & `function` allow redeclaration and are function-scoped (hoist).

---

<div class="post-metadata">

**Author:** ![jokovkilg](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jokovkilg](https://discourse.processing.org/u/jokovkilg)\
**Post date:** [November 25, 2021, 1:34am UTC](https://discourse.processing.org/t/when-do-you-need-var-can-you-only-use-let-to-declare-variables/19673/5 "2021-11-25T01:34:35Z")

</div>

Variables declared by “var” keyword are scoped to the immediate function body (function scope) while “let” variables are scoped to the immediate enclosing block denoted by {…} (block scope).

Variable declared with var keyword can be re-declared and updated in the same scope while variable declared with [let](http://net-informations.com/q/js/let.html) keyword can be updated but not re-declared.

Variables defined with let are hoisted to the top of the block , but not initialized. This means that the block of code is aware of the variable, but it cannot be used until it has been declared. So, using a let variable before it is declared will result in a ReferenceError.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 25, 2021, 2:42am UTC](https://discourse.processing.org/t/when-do-you-need-var-can-you-only-use-let-to-declare-variables/19673/6 "2021-11-25T02:42:50Z")

</div>

> [@jokovkilg](#):
>
> Variables defined with `let` are hoisted to the top of the block,

AFAIK all function’s parameters plus its local variables are “hoisted” not only in JavaScript but any programming language!

That is, when a function is invoked, a stack memory block is allocated w/ enough room for all parameters & variables that function needs to operate.

Therefore, everything is already “hoisted” even before the function’s 1st line code is executed.

> [@jokovkilg](#):
>
> This means that the block of code is aware of the variable, but it cannot be used until it has been declared.

If a local variable is prohibited of being accessed before its declaring statement line it’s effectively moot whether an early block of code is “aware” of its existence!

In JS only those variables declared w/ `var` or `function` have their access permitted anywhere within their function’s body, even though variables declared using other keywords pre-exist as well.

> [@jokovkilg](#):
>
> , but not initialized.

Don’t forget keyword `function` both hoists & initializes the variable it declares w/ a function reference, while `var` hoists but initializes later.
