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

8
  • Have you tried to check the value of b after changing a? Commented Oct 24, 2020 at 13:58
  • @Professorval it would be unchanged Commented Oct 24, 2020 at 13:58
  • @OP what you want is function, rather than variables. The other way is an object with getters. Commented Oct 24, 2020 at 13:59
  • You can consider using computed property. Commented Oct 24, 2020 at 14:00
  • 1
    working with getters and setters can be interesting (complex but scalable solution): stackoverflow.com/questions/1759987/… Commented Oct 24, 2020 at 14:09

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer :)
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);

3 Comments

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?
Yeah, I prefer using const to declare variables whenever possible, see softwareengineering.stackexchange.com/questions/278652/… - 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)
@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 but mostly irrelevant here.
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 

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.