# Let or nothing?

**URL:** <https://discourse.processing.org/t/let-or-nothing/46937>\
**Category:** Beginners\
**Created:** [August 15, 2025, 9:47pm UTC](https://discourse.processing.org/t/let-or-nothing/46937 "2025-08-15T21:47:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricRogerGarcia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ericrogergarcia/32/21029_2.png) [@EricRogerGarcia](https://discourse.processing.org/u/EricRogerGarcia)\
**Post date:** [August 15, 2025, 9:47pm UTC](https://discourse.processing.org/t/let-or-nothing/46937/1 "2025-08-15T21:47:56Z")

</div>

Hello,

If I create a new variable, I ‘m going to write  
`let thing = 5;`  
for example.

But if I write  
`thing = 5;`  
(while I haven’t already created a variable ‘thing’)  
that works too!

is there a difference?

🙂

---

<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:** [August 15, 2025, 11:05pm UTC](https://discourse.processing.org/t/let-or-nothing/46937/2 "2025-08-15T23:05:01Z")

</div>

That behavior is nicknamed “sloppy mode”:

> **[Sloppy mode - Glossary | MDN](https://developer.mozilla.org/en-US/docs/Glossary/Sloppy_mode)**
>
> ECMAScript 5 and later let scripts opt in to a new strict mode, which alters the semantics of JavaScript in several ways to improve its resiliency and which make it easier to understand what's going on when there are problems.

In order to activate [“strict mode”](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode), place `"use strict";` as the 1st non-comment statement in a JS file.

We can also activate “strict mode” by loading a JS file as an [“ECMA script module (ESM)”](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script/type#module).

---

<div class="post-metadata">

**Author:** ![EricRogerGarcia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ericrogergarcia/32/21029_2.png) [@EricRogerGarcia](https://discourse.processing.org/u/EricRogerGarcia)\
**Post date:** [August 16, 2025, 6:52am UTC](https://discourse.processing.org/t/let-or-nothing/46937/3 "2025-08-16T06:52:50Z")

</div>

Thank you…

so, I answer my question (if I understood correctly!):

If I write in a method (or function)  
`let thing=3;`  
then thing is local to the method

but if I write  
`thing=3`  
then thing is global

it’s silly!

**… so I absolutely have to (re)put ‘let’ everywhere!**

😶‍🌫️

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 16, 2025, 10:11am UTC](https://discourse.processing.org/t/let-or-nothing/46937/4 "2025-08-16T10:11:53Z")

</div>

Prior to ES6 variables in JS only had **Global Scope** or **Function Scope**. ES6 introduced the **Block Scope** with two new keywords `let` and `const`. A block is defined by curly braces `{ }`.

This code has three blocks -

```auto
function foo() { // Block 1
  let a = 9;
  if(a != a) { // Block 2
    let b = 3;
  }
  else { // Block 3
    let c = 5;
  }
}

```

Variable `b` is only visible in block `2`  
Variable `c` is only visible in block `3`  
Variable ‘a’ is visible in all three blocks because blocks `2` and `3` are inside the scope of block `1`

Whether you use `let` and `const` is up to you but I wouldn’t call it silly because their introduction is to help users create stable programs.

Using global variables should be avoided as it can introduce logic errors by accidentally changing values in global variables in some far distant part of the program simply due to a mistyped variable name. _ **Logic errors are by far the hardest to find and fix.** _

Strict mode takes this a step further by requiring variables be declared before assignment

```auto
let x; // declaration
x = 999;` // assignment

```

These require more work from the programmer but if you are creating software of any significant size or longevity it is well worth the effort.

---

<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:** [August 16, 2025, 10:20am UTC](https://discourse.processing.org/t/let-or-nothing/46937/5 "2025-08-16T10:20:32Z")

</div>

My advice is to always place a `"use strict";` at the top of each .js non-module file and to prioritize keyword `const` over `let` or `var` when declaring variables as much as possible.

> **[const - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)**
>
> The const declaration declares block-scoped local variables. The value of a constant can't be changed through reassignment using the assignment operator, but if a constant is an object, its properties can be added, updated, or removed.
