admin管理员组文章数量:1426644
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
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
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Feb 25, 2017 at 5:53 qw3nqw3n 6,3346 gold badges34 silver badges63 bronze badges 8- why do you need to do this? – Jaromanda X Commented Feb 25, 2017 at 5:55
- 1 Prefer readability over conciseness. Write code that won't make you scratch your head in a month. – 000 Commented Feb 25, 2017 at 5:57
-
if you want to initialize two variables to the same value:
var a = 2, b = a;
orvar 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 Commented Feb 25, 2017 at 5:58 -
1
a = b = 5;
does as expected (notvar a=b=5
though) ... adding the same value - not so easy – Jaromanda X Commented Feb 25, 2017 at 5: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 Commented Feb 25, 2017 at 5:59
2 Answers
Reset to default 5You 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 bees 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 pletion'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.
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);
本文标签: javascriptHow do you add the same value to two variables in one statmentStack Overflow
版权声明:本文标题:javascript - How do you add the same value to two variables in one statment? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745471432a2659768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论