2

It is possible to initialize multiple variables to the same value a=b=5. Also, it is possible to add to a variable with a+=2. But is there any way to add the same value to both at the same time? Something like this a+=b+=2 without the a getting incremented by the value of 2 and b. So in this case both a and b would end up being 7.

Also, I'm not looking for an answer that repeats the added value such as a+=2,b+=2.

I realize this is not the greatest coding practice I just wondered if it was possible. Somewhat similar in nature to Swapping two variable value without using 3rd variable

Community
  • 1
  • 1
qw3n
  • 6,236
  • 6
  • 33
  • 62
  • why do you need to do this? – Jaromanda X Feb 25 '17 at 05:55
  • 1
    Prefer readability over conciseness. Write code that won't make you scratch your head in a month. – 000 Feb 25 '17 at 05:57
  • if you want to initialize two variables to the same value: `var a = 2, b = a;` or `var a,b; a=b=2;` to add the same value to two variables, use a third variable: `var amount = 2; a += amount, b += amount`; That's what vars are for, to store values. – Thomas Feb 25 '17 at 05:58
  • 1
    `a = b = 5;` does as expected (not `var a=b=5` though) ... adding the same value - not so easy – Jaromanda X Feb 25 '17 at 05:59
  • I don't necessarily need it. It probably would make the code less maintainable, but I was curious to see if it was possible. My scenario is I have two properties that I need to increment by the same amount, but that amount is a derived from a ternary statement. – qw3n Feb 25 '17 at 05:59
  • Not sure what expected result is? _"My scenario is I have two properties that I need to increment by the same amount, but that amount is a derived from a ternary statement."_ Can you include `javascript` that you have tried at Question? – guest271314 Feb 25 '17 at 05:59
  • I realised, that's why I removed the comment @Thomas :p – Jaromanda X Feb 25 '17 at 06:06
  • @guest271314 something like `var a=5,b=7; ` Then you want to add the result of `foo?2:3`. So if false the result would be 8 and 10. – qw3n Feb 25 '17 at 06:12

2 Answers2

5

You can do it! Here's how to:

var a = 10;
var b = 15;
b += -a + (a += 10);  // In this case, adding 10 to both
console.log(a, b);  //=> 20 25

BOOM (yes it blew my own mind too)

Sometimes it doesn't matter why you do it, it just matters that you can do it!


So, how / why does it work?

The -a gets parsed and becomes the negative value of a, which is 10 (so -10). Then, a += 10 runs and sets a to 20 (10 + 10). Finally, it sums up both values (-10 + 20) and gives you the difference between old a and new a.

Gorgeous.


EDIT: For completion's sake, as of ES6's destructuring assignments, you could do:

let a = 10;
let b = 15;
[ a, b ] = [ a+10, b+10 ]
console.log(a, b);        //=> 20 25

Which is also one statement, as well as being tidier and simpler to understand.

Matheus Avellar
  • 1,507
  • 1
  • 22
  • 29
  • 1
    By the way, thanks a lot for the thought exercise, I absolutely love these kinds of things. Just made my night! – Matheus Avellar Feb 25 '17 at 06:05
  • 1
    I was so close... Very nice! Too bad it isn't actually shorter than just using += twice for code golf. – Gerrit0 Feb 25 '17 at 06:10
  • 1
    Very nice I was thinking there had to be some mathematically way. To bad it doesn't work for strings too. – qw3n Feb 25 '17 at 06:12
2

So in this case both a and b would end up being 7.

You can add the two defined variables and the specified incremented value multiplied by the number of variables then divide by the number of variables

a = b = 5;
console.log(a, b);
n = 2;
a = b = ((a + b + n * 2) / 2);
console.log(a, b);

something like var a=5,b=7; Then you want to add the result of foo?2:3. So if false the result would be 8 and 10.

You can use destructuring assignment, Array.prototype.map()

var a = 5, b = 7;
console.log(a, b);
var foo = false;
[a, b] = [a, b].map(v => v += foo ? 2 : 3);
console.log(a, b);
guest271314
  • 1
  • 15
  • 104
  • 177