# new variable without any type should a let

**URL:** <https://es.discourse.group/t/new-variable-without-any-type-should-a-let/1454>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [September 7, 2022, 9:37pm UTC](https://es.discourse.group/t/new-variable-without-any-type-should-a-let/1454 "2022-09-07T21:37:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![1C0D](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/1c0d/32/1495_2.png) [@1C0D](https://es.discourse.group/u/1C0D)\
**Post date:** [September 7, 2022, 9:37pm UTC](https://es.discourse.group/t/new-variable-without-any-type-should-a-let/1454/1 "2022-09-07T21:37:16Z")

</div>

creating a new variable like this:  
a=42;  
should result in a let instead of a var.  
e.g  
for (prop in user) {console.log("no problem of scope and very helpful");  
}

---

<div class="post-metadata">

**Author:** ![ptomato](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ptomato/32/214_2.png) [@ptomato](https://es.discourse.group/u/ptomato)\
**Post date:** [September 7, 2022, 10:18pm UTC](https://es.discourse.group/t/new-variable-without-any-type-should-a-let/1454/2 "2022-09-07T22:18:50Z")

</div>

It seems to me like this would break existing code... currently, `a = 42;` when there's no `a` in scope creates a variable in the global scope. So, it's likely there's code on the web that relies on that behaviour. For example, the following code snippet would change its behaviour:

```nohighlight
function foo() {
  a = 42;
}
function bar() {
  console.log(a);
}

```

Currently, this would log 42. With that change, it would log undefined.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [September 8, 2022, 2:01am UTC](https://es.discourse.group/t/new-variable-without-any-type-should-a-let/1454/3 "2022-09-08T02:01:35Z")

</div>

An assignment to a new variable without a declaration should throw, not implicitly create a new variable in any scope.  
And it does, in strict mode!

```javascript
"use strict";
for (prop in user) {
   console.log("no problem of scope and very helpful");
}

```

-\> _Uncaught ReferenceError: prop is not defined_

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [September 8, 2022, 2:02am UTC](https://es.discourse.group/t/new-variable-without-any-type-should-a-let/1454/4 "2022-09-08T02:02:57Z")

</div>

The solution here is indeed to use a linter to enforce the last decades of best practices of being explicit and always including a declaration keyword.

---

<div class="post-metadata">

**Author:** ![Rudxain](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rudxain/32/2272_2.png) [@Rudxain](https://es.discourse.group/u/Rudxain)\
**Post date:** [September 8, 2022, 3:43am UTC](https://es.discourse.group/t/new-variable-without-any-type-should-a-let/1454/5 "2022-09-08T03:43:40Z")

</div>

The fact that "short assignment" behaves the way it does is an example of [technical debt](https://en.wikipedia.org/wiki/Technical_debt). **ES/JS cannot change this easily** , because a lot of legacy code (and minifiers) depends on this behavior.

One of the only ways would be to have a "heads-up announcement/alert" that notifies all devs and websites of future breaking-changes, and have a transition period to give time for everyone to prepare. Unfortunately, it's unlikely that will ever happen
