Write-only variables !

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"

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.

2 Likes

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,

(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.

2 Likes

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.

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

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