Add a Number to Two Variables At Once

You can initialize two variables to the same value at once, kinda:

var foo, bar;
foo = bar = 10;

But there is no similarly easy mechanism to add, say, 5 to both foo and bar at the same time. Of course, it's simple enough to just do:

foo += 5; // foo is now 15
bar += 5; // bar is now 15

But that's two operations.

The trick is:

foo += -bar + (bar += 5);
// foo and bar are now 15

You will probably never need this, it's just interesting to know it's possible.

Thanks to Matheus Avellar for sending in this little mindbending trick, who also explains:

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

Comments

  1. User Avatar
    Ryan Van Etten
    Permalink to comment#

    Or like this :)

    foo = bar += 5
    
    • User Avatar
      William Ott
      Permalink to comment#

      ‘foo = bar += 5’ would just set ‘foo’ equal to ‘bar + 5’.

    • User Avatar
      Kevin
      Permalink to comment#

      @William, this is what Chris wants. He wants to initialize both variables with 10:

      var foo, bar;
      foo = bar = 10;
      
      console.log(foo, bar); // 10, 10
      

      Then add 5 to each of them:

      foo = bar += 5; // same thing as: bar += 5; foo = bar;
      console.log(foo, bar); // 15, 15
      

      Every math operation returns its new value:

      var hello = 10;
      console.log(hello += 5); // 15
      
    • User Avatar
      Drazen
      Permalink to comment#

      @Kevin in the example he set both to 10 but the exercise is all about what if you have 2 different variables and you want them both to go up by 5.

      var foo = 10; //will be 15
      var bar = 20; //will be 25
      
      foo = bar += 5 //will result in both being 25
      foo += -bar + (bar += 5); //will result in foo 15 and bar 25
      
    • User Avatar
      Joe Maffei
      Permalink to comment#

      It’s stuff like this that supports Douglas Crockford’s idea of making JavaScript fully extensible: “With the same ease that we can define new variables, we (could) let the programmer add new operators and new statements.” http://javascript.crockford.com/tdop/tdop.html

    • User Avatar
      Nathen

      Yup, that works fine, thanks for the tip. I don’t understand what William is saying by telling that it wont work.

  2. User Avatar
    Michał Sadowski
    Permalink to comment#

    If I saw something like that in code I’d probably spend next two hours trying to understand why.

    • User Avatar
      Joe Maffei
      Permalink to comment#

      Code like this is written only for education, not production.

  3. User Avatar
    Claude
    Permalink to comment#

    You say about foo += 5; bar += 5;:

    But that’s two operations.

    However, foo += -bar + (bar += 5); is worse in that regard, since that’s FOUR operations. You could reduce it to three with foo -= bar - (bar += 5), with the added advantage of making your code even more inscrutable.

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag