4

I think this is a simple one, and it may even have been asked previously, but I don't know what keywords to use to do a search. I believe that variables are strings and can only be strings, so even my question is a poor one, but I want to figure out a good way to do some math.

We start with something simple, like:

var a=0, b=a+1
console.log(a,b) // yields 0,1

But I then want to be able to update a later in the code and have it automatically update b, so if later I set:

a=1
console.log(a,b) // now yields 1,2

The goal being the updated b without having to tell the code that b=a+1 a second time (or hundreds of times, as this is a lot of math I am playing with).

Thomas
  • 392
  • 4
  • 13
  • Have you tried to check the value of b after changing a? – Professorval Oct 24 '20 at 13:58
  • @Professorval it would be unchanged – VLAZ Oct 24 '20 at 13:58
  • @OP what you want is function, rather than variables. The other way is an object with getters. – VLAZ Oct 24 '20 at 13:59
  • You can consider using computed property. – Thunderbolt Engineer Oct 24 '20 at 14:00
  • there are several ways to do what you want in javascript. try searching for "reactive programming" or "proxy". – Dan O Oct 24 '20 at 14:01
  • It seems to me that you are looking for something resembling an Excel spreadsheet. You work with values (a) and formulae (b) that are updated "automatically". – Carsten Massmann Oct 24 '20 at 14:03
  • I admit, I am a bit of an old timer. Lots of new ideas being presented here, thanks. I admit that Excel is more comfortable to me than javascript. I learned javascript a decade ago and am still back there in my knowledge. – Thomas Oct 24 '20 at 14:05
  • 1
    working with getters and setters can be interesting (complex but scalable solution): https://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript – Quentin C Oct 24 '20 at 14:09

4 Answers4

3

Instead of using a variable you could use an object with a getter and setter.

const obj = {
  a: 0,
  get b() { return this.a + 1 },
  set b(value) { this.a = value - 1 },
};

console.log(obj);
obj.a = 10;
console.log(obj);
obj.b = 20;
console.log(obj);

If you never plan to set b, then you can omit the setter.

3limin4t0r
  • 13,832
  • 1
  • 17
  • 33
2

Create a separate function that updates both variables. Then, whenever you want to modify them, call the method instead of reassigning the variables manually, for example:

var a = 0,
  b = a + 1;

const increment = () => {
  a++;
  b++;
};
increment();
increment();
console.log(a, b);
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
  • This ```const``` is new to me, but only because I have not kept up to date. It appears to basically be a function, but one that cannot be changed/updated? – Thomas Oct 24 '20 at 14:09
  • 1
    Yeah, I prefer using `const` to declare variables whenever possible, see https://softwareengineering.stackexchange.com/questions/278652/how-much-should-i-be-using-let-vs-const-in-es6 - it makes code more readable at a glance when you can immediately determine whether something may possibly be reassigned or not. Arrow functions are also much more concise than full `functions` in many situations (though not this one) – CertainPerformance Oct 24 '20 at 14:10
  • 1
    @Thomas `const` is like `var` in that it declares a variable. However, that variable cannot be reassigned. the `() => {}` part is a function. It's the arrow function syntax (named after the `=>` part) and works basically like using `function() {}` but it's shorter. [There are few other differences](https://stackoverflow.com/q/34361379/) but mostly irrelevant here. – VLAZ Oct 24 '20 at 14:33
1

I would use a function for b instead and call it whenever is needed.

let a = 1;
const b = () => a + 1;
console.log(b()); // prints 2
a = 10;
console.log(b()); // prints 11
Gabriel Lupu
  • 796
  • 1
  • 5
  • 21
1

You can create a function to increment both the values.

const func= () => {
  a++;
  b++;
};

And then you can call the function each time you have to increment values. If you have to increment continuously for a fixed number of times then call the function inside a loop.

Drew
  • 3,647
  • 3
  • 20
  • 37
Ankit Jaishwal
  • 126
  • 1
  • 7