# Write-only variables !

**URL:** <https://es.discourse.group/t/write-only-variables/563>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [December 2, 2020, 7:43am UTC](https://es.discourse.group/t/write-only-variables/563 "2020-12-02T07:43:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seagat2011](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seagat2011/32/367_2.png) [@Seagat2011](https://es.discourse.group/u/Seagat2011)\
**Post date:** [December 2, 2020, 7:43am UTC](https://es.discourse.group/t/write-only-variables/563/1 "2020-12-02T07:43:50Z")

</div>

```javascript
Variables which cannot be read at the JS/interpreter level !

let myArray = [,,,,,"5","6"];
let myObject = { 5:"five",6:"six" }

let a := (5,"salt") // a == 5 !
a == 5 // ReferenceError: a is not defined !
(a) == 5 // false
(a + "salt") == 5 // false
a + 3 // 3
(a + "salt") + 3 // 3
myArray[a] // undefined
myObject[a] // undefined
myArray[(a + "salt")] // "5"
myObject[(a + "salt")] // "five"
console.log(`The value of a is ${ a } ${ (a + "salt") }`) // "The value of a is undefined undefined"

a := 6 // undefined
a := (6,"salt") // a == 6 !
a == 6 // ReferenceError: a is not defined !
myArray[a] // undefined
myObject[a] // undefined
myArray[(a + "salt")] // "6"
myObject[(a + "salt")] // "six"

let b = (a + "salt") // "salt"

```

---

<div class="post-metadata">

**Author:** ![Yaffle](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/yaffle/32/900_2.png) [@Yaffle](https://es.discourse.group/u/Yaffle)\
**Post date:** [December 2, 2020, 10:31am UTC](https://es.discourse.group/t/write-only-variables/563/2 "2020-12-02T10:31:34Z")

</div>

it is unclear what you are doing,  
The variables defined using "const" keyword are write only. Scope variables are pretty private, there are also Symbol and WeakMap for some "strange to me" use cases.  
ReferenceError is typically thrown independently of the place of the identifier.

---

<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:** [December 2, 2020, 10:58am UTC](https://es.discourse.group/t/write-only-variables/563/3 "2020-12-02T10:58:44Z")

</div>

It's really unclear what "write-only" means to you, what this would be good for, and how your `:=` and `+` operators are supposed to work. Please post a description of the feature, not just some demo code.

Also,

```javascript
(a + "salt") == 5 // false
// contrasted against
const myObject = { 5:"five",6:"six" };
myObject[(a + "salt")] // "five"

```

defies the laws of JavaScript regardless what `(a + "salt")` evaluates to.

---

<div class="post-metadata">

**Author:** ![JackWorks](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jackworks/32/260_2.png) [@JackWorks](https://es.discourse.group/u/JackWorks)\
**Post date:** [January 11, 2021, 7:36am UTC](https://es.discourse.group/t/write-only-variables/563/4 "2021-01-11T07:36:01Z")

</div>

What can you do with a "write-only" variable if you never read it? In TypeScript this kind of variable (write, with no read) is treated as an unused variable.

---

<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 18, 2022, 4:53am UTC](https://es.discourse.group/t/write-only-variables/563/5 "2022-09-18T04:53:12Z")

</div>

This is interesting because it's like the variable is _"locked"_ by a key/password (in this case `"salt"`). But for me, this is controversial, I doubt it will be useful, except in very niche special cases

---

<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 18, 2022, 4:56am UTC](https://es.discourse.group/t/write-only-variables/563/6 "2022-09-18T04:56:09Z")

</div>

> [@Yaffle](#):
>
> Scope variables are pretty private

Yes, but they require indentation (I know indentation is not mandatory, but it's an enforced good practice), so every local variable that we want to limit its lifetime will add 1 indent level (in some cases, not always). This is an unnecessary "penalty" that discourages the good practice of limited lifetimes and low var-count within a given scope.

However, I agree with everything else you said
