JavaScript strange behavior with operator precedence!!!1










-1















What do you think will be result of this expression?






var a = 10;
a = a + (a = 5);
console.log(a);

// a = 10?





NO!!! It's 15!



Now lets look at the other similar statement:






var a = 10;
a = (a = 5) + a;
console.log(a);

// a = 15?





Again no, now its 10. Why is that? I cant understand. But wait, there is two more:






var a = 10;
a = a + a++;
console.log(a);

// a = 21? Haha no! Its 20!





And the last one:






var a = 10;
a = a++ + a;
console.log(a);

// 21))) now my mind is blown away (





So can anybody tell, why javascript behaves this way?










share|improve this question
























  • I just wonder why they are correct, i dont say they aren't

    – Nikol Lakin
    Nov 14 '18 at 10:25











  • what about first two examples?

    – Nikol Lakin
    Nov 14 '18 at 10:27






  • 1





    While all the questions you have are grounded in the same basic issue (What order are operations carried out in when I assign a value to the same variable multiple times in the same expression? — which is a terrible idea in the first place ), I'm voting to close this as too broad because they are separate problems (and there are probably separate duplicates for each issue floating around anyway).

    – Quentin
    Nov 14 '18 at 10:28







  • 1





    More accessible references: stackoverflow.com/q/34094916/218196, stackoverflow.com/q/50869790/218196

    – Felix Kling
    Nov 14 '18 at 10:36







  • 1





    "The expresion in parentheses must evaluate first?" That's not what parenthesis mean. I guess one could say that they impact the order of evaluation (but that does not necessarily mean that they are evaluated first, just imagine you have multiple groups of parenthesis, they cannot be "first" all together). Rather they change the precedence of operands/operators and operators, which as a side effect impacts evaluation order. E.g. 3 * 4 + 5 is different than 3 * (4 + 5). The LHS of x * y, i.e. 3 is still evaluated first. The difference is in evaluating the RHS, 4 vs 4 + 5.

    – Felix Kling
    Nov 14 '18 at 10:44
















-1















What do you think will be result of this expression?






var a = 10;
a = a + (a = 5);
console.log(a);

// a = 10?





NO!!! It's 15!



Now lets look at the other similar statement:






var a = 10;
a = (a = 5) + a;
console.log(a);

// a = 15?





Again no, now its 10. Why is that? I cant understand. But wait, there is two more:






var a = 10;
a = a + a++;
console.log(a);

// a = 21? Haha no! Its 20!





And the last one:






var a = 10;
a = a++ + a;
console.log(a);

// 21))) now my mind is blown away (





So can anybody tell, why javascript behaves this way?










share|improve this question
























  • I just wonder why they are correct, i dont say they aren't

    – Nikol Lakin
    Nov 14 '18 at 10:25











  • what about first two examples?

    – Nikol Lakin
    Nov 14 '18 at 10:27






  • 1





    While all the questions you have are grounded in the same basic issue (What order are operations carried out in when I assign a value to the same variable multiple times in the same expression? — which is a terrible idea in the first place ), I'm voting to close this as too broad because they are separate problems (and there are probably separate duplicates for each issue floating around anyway).

    – Quentin
    Nov 14 '18 at 10:28







  • 1





    More accessible references: stackoverflow.com/q/34094916/218196, stackoverflow.com/q/50869790/218196

    – Felix Kling
    Nov 14 '18 at 10:36







  • 1





    "The expresion in parentheses must evaluate first?" That's not what parenthesis mean. I guess one could say that they impact the order of evaluation (but that does not necessarily mean that they are evaluated first, just imagine you have multiple groups of parenthesis, they cannot be "first" all together). Rather they change the precedence of operands/operators and operators, which as a side effect impacts evaluation order. E.g. 3 * 4 + 5 is different than 3 * (4 + 5). The LHS of x * y, i.e. 3 is still evaluated first. The difference is in evaluating the RHS, 4 vs 4 + 5.

    – Felix Kling
    Nov 14 '18 at 10:44














-1












-1








-1








What do you think will be result of this expression?






var a = 10;
a = a + (a = 5);
console.log(a);

// a = 10?





NO!!! It's 15!



Now lets look at the other similar statement:






var a = 10;
a = (a = 5) + a;
console.log(a);

// a = 15?





Again no, now its 10. Why is that? I cant understand. But wait, there is two more:






var a = 10;
a = a + a++;
console.log(a);

// a = 21? Haha no! Its 20!





And the last one:






var a = 10;
a = a++ + a;
console.log(a);

// 21))) now my mind is blown away (





So can anybody tell, why javascript behaves this way?










share|improve this question
















What do you think will be result of this expression?






var a = 10;
a = a + (a = 5);
console.log(a);

// a = 10?





NO!!! It's 15!



Now lets look at the other similar statement:






var a = 10;
a = (a = 5) + a;
console.log(a);

// a = 15?





Again no, now its 10. Why is that? I cant understand. But wait, there is two more:






var a = 10;
a = a + a++;
console.log(a);

// a = 21? Haha no! Its 20!





And the last one:






var a = 10;
a = a++ + a;
console.log(a);

// 21))) now my mind is blown away (





So can anybody tell, why javascript behaves this way?






var a = 10;
a = a + (a = 5);
console.log(a);

// a = 10?





var a = 10;
a = a + (a = 5);
console.log(a);

// a = 10?





var a = 10;
a = (a = 5) + a;
console.log(a);

// a = 15?





var a = 10;
a = (a = 5) + a;
console.log(a);

// a = 15?





var a = 10;
a = a + a++;
console.log(a);

// a = 21? Haha no! Its 20!





var a = 10;
a = a + a++;
console.log(a);

// a = 21? Haha no! Its 20!





var a = 10;
a = a++ + a;
console.log(a);

// 21))) now my mind is blown away (





var a = 10;
a = a++ + a;
console.log(a);

// 21))) now my mind is blown away (






javascript operators operator-precedence






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 10:46









B001ᛦ

1,14251320




1,14251320










asked Nov 14 '18 at 10:20









Nikol LakinNikol Lakin

11




11












  • I just wonder why they are correct, i dont say they aren't

    – Nikol Lakin
    Nov 14 '18 at 10:25











  • what about first two examples?

    – Nikol Lakin
    Nov 14 '18 at 10:27






  • 1





    While all the questions you have are grounded in the same basic issue (What order are operations carried out in when I assign a value to the same variable multiple times in the same expression? — which is a terrible idea in the first place ), I'm voting to close this as too broad because they are separate problems (and there are probably separate duplicates for each issue floating around anyway).

    – Quentin
    Nov 14 '18 at 10:28







  • 1





    More accessible references: stackoverflow.com/q/34094916/218196, stackoverflow.com/q/50869790/218196

    – Felix Kling
    Nov 14 '18 at 10:36







  • 1





    "The expresion in parentheses must evaluate first?" That's not what parenthesis mean. I guess one could say that they impact the order of evaluation (but that does not necessarily mean that they are evaluated first, just imagine you have multiple groups of parenthesis, they cannot be "first" all together). Rather they change the precedence of operands/operators and operators, which as a side effect impacts evaluation order. E.g. 3 * 4 + 5 is different than 3 * (4 + 5). The LHS of x * y, i.e. 3 is still evaluated first. The difference is in evaluating the RHS, 4 vs 4 + 5.

    – Felix Kling
    Nov 14 '18 at 10:44


















  • I just wonder why they are correct, i dont say they aren't

    – Nikol Lakin
    Nov 14 '18 at 10:25











  • what about first two examples?

    – Nikol Lakin
    Nov 14 '18 at 10:27






  • 1





    While all the questions you have are grounded in the same basic issue (What order are operations carried out in when I assign a value to the same variable multiple times in the same expression? — which is a terrible idea in the first place ), I'm voting to close this as too broad because they are separate problems (and there are probably separate duplicates for each issue floating around anyway).

    – Quentin
    Nov 14 '18 at 10:28







  • 1





    More accessible references: stackoverflow.com/q/34094916/218196, stackoverflow.com/q/50869790/218196

    – Felix Kling
    Nov 14 '18 at 10:36







  • 1





    "The expresion in parentheses must evaluate first?" That's not what parenthesis mean. I guess one could say that they impact the order of evaluation (but that does not necessarily mean that they are evaluated first, just imagine you have multiple groups of parenthesis, they cannot be "first" all together). Rather they change the precedence of operands/operators and operators, which as a side effect impacts evaluation order. E.g. 3 * 4 + 5 is different than 3 * (4 + 5). The LHS of x * y, i.e. 3 is still evaluated first. The difference is in evaluating the RHS, 4 vs 4 + 5.

    – Felix Kling
    Nov 14 '18 at 10:44

















I just wonder why they are correct, i dont say they aren't

– Nikol Lakin
Nov 14 '18 at 10:25





I just wonder why they are correct, i dont say they aren't

– Nikol Lakin
Nov 14 '18 at 10:25













what about first two examples?

– Nikol Lakin
Nov 14 '18 at 10:27





what about first two examples?

– Nikol Lakin
Nov 14 '18 at 10:27




1




1





While all the questions you have are grounded in the same basic issue (What order are operations carried out in when I assign a value to the same variable multiple times in the same expression? — which is a terrible idea in the first place ), I'm voting to close this as too broad because they are separate problems (and there are probably separate duplicates for each issue floating around anyway).

– Quentin
Nov 14 '18 at 10:28






While all the questions you have are grounded in the same basic issue (What order are operations carried out in when I assign a value to the same variable multiple times in the same expression? — which is a terrible idea in the first place ), I'm voting to close this as too broad because they are separate problems (and there are probably separate duplicates for each issue floating around anyway).

– Quentin
Nov 14 '18 at 10:28





1




1





More accessible references: stackoverflow.com/q/34094916/218196, stackoverflow.com/q/50869790/218196

– Felix Kling
Nov 14 '18 at 10:36






More accessible references: stackoverflow.com/q/34094916/218196, stackoverflow.com/q/50869790/218196

– Felix Kling
Nov 14 '18 at 10:36





1




1





"The expresion in parentheses must evaluate first?" That's not what parenthesis mean. I guess one could say that they impact the order of evaluation (but that does not necessarily mean that they are evaluated first, just imagine you have multiple groups of parenthesis, they cannot be "first" all together). Rather they change the precedence of operands/operators and operators, which as a side effect impacts evaluation order. E.g. 3 * 4 + 5 is different than 3 * (4 + 5). The LHS of x * y, i.e. 3 is still evaluated first. The difference is in evaluating the RHS, 4 vs 4 + 5.

– Felix Kling
Nov 14 '18 at 10:44






"The expresion in parentheses must evaluate first?" That's not what parenthesis mean. I guess one could say that they impact the order of evaluation (but that does not necessarily mean that they are evaluated first, just imagine you have multiple groups of parenthesis, they cannot be "first" all together). Rather they change the precedence of operands/operators and operators, which as a side effect impacts evaluation order. E.g. 3 * 4 + 5 is different than 3 * (4 + 5). The LHS of x * y, i.e. 3 is still evaluated first. The difference is in evaluating the RHS, 4 vs 4 + 5.

– Felix Kling
Nov 14 '18 at 10:44













2 Answers
2






active

oldest

votes


















1














To understand this better, let us consider another variable



CASE 1






var a = 10;
a = a + (a = 5);
console.log(a); // 15

a = 10;
var b = a + (a = 5);
console.log(a); // 5
console.log(b); // 15





a + (a = 5)
5 + (a = 5) // here a is assigned a value 5 and bracket returns 5
5 + 5 // here value of a is 5
10 // value will be assigned to variable on LHS


CASE 2






var a = 10;
a = (a = 5) + a;
console.log(a); // 10


a = 10;
var b = (a = 5) + a;
console.log(a); // 5
console.log(b); // 10





(a = 5) + a
(a = 5) + a // here a is assigned a value 5 and bracket returns value 5
5 + 5 // here value of a is 5
10 // value will be assigned to variable on LHS


CASE 3






var a = 10;
a = a + a++;
console.log(a); // 20

a = 10;
var b = a + a++;
console.log(a); // 11
console.log(b); // 20





a + a++ // post increment i.e. it will return the value first and then increment
10 + 10 // where value of a will 10 + 1 i.e. 11
20 // value will be assigned to variable on LHS


CASE 4






var a = 10;
a = a++ + a;
console.log(a); // 21

a = 10;
var b = a++ + a;
console.log(a); // 11
console.log(b); // 21





a++ + a // post increment i.e. it will return the value first and then increment
10 + a // where value of a will 10 + 1 i.e. 11
10 + 11 // as value of a is 11
21 // value will be assigned to variable on LHS





share|improve this answer






























    0

















    var a = 10;
    a = a + (a = 5);
    console.log(a);

    // a = 10?





    It's 15 because the value of a was 10 and you added (a = 5) after, which changed a's value to 5. Therefore, you did 10+5.






    var a = 10;
    a = (a = 5) + a;
    console.log(a);

    // a = 15?





    Now that you have (a = 5) first, you're changing its value to 5 before adding a. Therefore, you're doing 5+5.



    Hope this helps.






    share|improve this answer























    • But the expression in parentheses must be evaluated first, i thought that what operator precedence mean

      – Nikol Lakin
      Nov 14 '18 at 11:16











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297856%2fjavascript-strange-behavior-with-operator-precedence1%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    To understand this better, let us consider another variable



    CASE 1






    var a = 10;
    a = a + (a = 5);
    console.log(a); // 15

    a = 10;
    var b = a + (a = 5);
    console.log(a); // 5
    console.log(b); // 15





    a + (a = 5)
    5 + (a = 5) // here a is assigned a value 5 and bracket returns 5
    5 + 5 // here value of a is 5
    10 // value will be assigned to variable on LHS


    CASE 2






    var a = 10;
    a = (a = 5) + a;
    console.log(a); // 10


    a = 10;
    var b = (a = 5) + a;
    console.log(a); // 5
    console.log(b); // 10





    (a = 5) + a
    (a = 5) + a // here a is assigned a value 5 and bracket returns value 5
    5 + 5 // here value of a is 5
    10 // value will be assigned to variable on LHS


    CASE 3






    var a = 10;
    a = a + a++;
    console.log(a); // 20

    a = 10;
    var b = a + a++;
    console.log(a); // 11
    console.log(b); // 20





    a + a++ // post increment i.e. it will return the value first and then increment
    10 + 10 // where value of a will 10 + 1 i.e. 11
    20 // value will be assigned to variable on LHS


    CASE 4






    var a = 10;
    a = a++ + a;
    console.log(a); // 21

    a = 10;
    var b = a++ + a;
    console.log(a); // 11
    console.log(b); // 21





    a++ + a // post increment i.e. it will return the value first and then increment
    10 + a // where value of a will 10 + 1 i.e. 11
    10 + 11 // as value of a is 11
    21 // value will be assigned to variable on LHS





    share|improve this answer



























      1














      To understand this better, let us consider another variable



      CASE 1






      var a = 10;
      a = a + (a = 5);
      console.log(a); // 15

      a = 10;
      var b = a + (a = 5);
      console.log(a); // 5
      console.log(b); // 15





      a + (a = 5)
      5 + (a = 5) // here a is assigned a value 5 and bracket returns 5
      5 + 5 // here value of a is 5
      10 // value will be assigned to variable on LHS


      CASE 2






      var a = 10;
      a = (a = 5) + a;
      console.log(a); // 10


      a = 10;
      var b = (a = 5) + a;
      console.log(a); // 5
      console.log(b); // 10





      (a = 5) + a
      (a = 5) + a // here a is assigned a value 5 and bracket returns value 5
      5 + 5 // here value of a is 5
      10 // value will be assigned to variable on LHS


      CASE 3






      var a = 10;
      a = a + a++;
      console.log(a); // 20

      a = 10;
      var b = a + a++;
      console.log(a); // 11
      console.log(b); // 20





      a + a++ // post increment i.e. it will return the value first and then increment
      10 + 10 // where value of a will 10 + 1 i.e. 11
      20 // value will be assigned to variable on LHS


      CASE 4






      var a = 10;
      a = a++ + a;
      console.log(a); // 21

      a = 10;
      var b = a++ + a;
      console.log(a); // 11
      console.log(b); // 21





      a++ + a // post increment i.e. it will return the value first and then increment
      10 + a // where value of a will 10 + 1 i.e. 11
      10 + 11 // as value of a is 11
      21 // value will be assigned to variable on LHS





      share|improve this answer

























        1












        1








        1







        To understand this better, let us consider another variable



        CASE 1






        var a = 10;
        a = a + (a = 5);
        console.log(a); // 15

        a = 10;
        var b = a + (a = 5);
        console.log(a); // 5
        console.log(b); // 15





        a + (a = 5)
        5 + (a = 5) // here a is assigned a value 5 and bracket returns 5
        5 + 5 // here value of a is 5
        10 // value will be assigned to variable on LHS


        CASE 2






        var a = 10;
        a = (a = 5) + a;
        console.log(a); // 10


        a = 10;
        var b = (a = 5) + a;
        console.log(a); // 5
        console.log(b); // 10





        (a = 5) + a
        (a = 5) + a // here a is assigned a value 5 and bracket returns value 5
        5 + 5 // here value of a is 5
        10 // value will be assigned to variable on LHS


        CASE 3






        var a = 10;
        a = a + a++;
        console.log(a); // 20

        a = 10;
        var b = a + a++;
        console.log(a); // 11
        console.log(b); // 20





        a + a++ // post increment i.e. it will return the value first and then increment
        10 + 10 // where value of a will 10 + 1 i.e. 11
        20 // value will be assigned to variable on LHS


        CASE 4






        var a = 10;
        a = a++ + a;
        console.log(a); // 21

        a = 10;
        var b = a++ + a;
        console.log(a); // 11
        console.log(b); // 21





        a++ + a // post increment i.e. it will return the value first and then increment
        10 + a // where value of a will 10 + 1 i.e. 11
        10 + 11 // as value of a is 11
        21 // value will be assigned to variable on LHS





        share|improve this answer













        To understand this better, let us consider another variable



        CASE 1






        var a = 10;
        a = a + (a = 5);
        console.log(a); // 15

        a = 10;
        var b = a + (a = 5);
        console.log(a); // 5
        console.log(b); // 15





        a + (a = 5)
        5 + (a = 5) // here a is assigned a value 5 and bracket returns 5
        5 + 5 // here value of a is 5
        10 // value will be assigned to variable on LHS


        CASE 2






        var a = 10;
        a = (a = 5) + a;
        console.log(a); // 10


        a = 10;
        var b = (a = 5) + a;
        console.log(a); // 5
        console.log(b); // 10





        (a = 5) + a
        (a = 5) + a // here a is assigned a value 5 and bracket returns value 5
        5 + 5 // here value of a is 5
        10 // value will be assigned to variable on LHS


        CASE 3






        var a = 10;
        a = a + a++;
        console.log(a); // 20

        a = 10;
        var b = a + a++;
        console.log(a); // 11
        console.log(b); // 20





        a + a++ // post increment i.e. it will return the value first and then increment
        10 + 10 // where value of a will 10 + 1 i.e. 11
        20 // value will be assigned to variable on LHS


        CASE 4






        var a = 10;
        a = a++ + a;
        console.log(a); // 21

        a = 10;
        var b = a++ + a;
        console.log(a); // 11
        console.log(b); // 21





        a++ + a // post increment i.e. it will return the value first and then increment
        10 + a // where value of a will 10 + 1 i.e. 11
        10 + 11 // as value of a is 11
        21 // value will be assigned to variable on LHS





        var a = 10;
        a = a + (a = 5);
        console.log(a); // 15

        a = 10;
        var b = a + (a = 5);
        console.log(a); // 5
        console.log(b); // 15





        var a = 10;
        a = a + (a = 5);
        console.log(a); // 15

        a = 10;
        var b = a + (a = 5);
        console.log(a); // 5
        console.log(b); // 15





        var a = 10;
        a = (a = 5) + a;
        console.log(a); // 10


        a = 10;
        var b = (a = 5) + a;
        console.log(a); // 5
        console.log(b); // 10





        var a = 10;
        a = (a = 5) + a;
        console.log(a); // 10


        a = 10;
        var b = (a = 5) + a;
        console.log(a); // 5
        console.log(b); // 10





        var a = 10;
        a = a + a++;
        console.log(a); // 20

        a = 10;
        var b = a + a++;
        console.log(a); // 11
        console.log(b); // 20





        var a = 10;
        a = a + a++;
        console.log(a); // 20

        a = 10;
        var b = a + a++;
        console.log(a); // 11
        console.log(b); // 20





        var a = 10;
        a = a++ + a;
        console.log(a); // 21

        a = 10;
        var b = a++ + a;
        console.log(a); // 11
        console.log(b); // 21





        var a = 10;
        a = a++ + a;
        console.log(a); // 21

        a = 10;
        var b = a++ + a;
        console.log(a); // 11
        console.log(b); // 21






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 10:47









        Nikhil AggarwalNikhil Aggarwal

        24k32747




        24k32747























            0

















            var a = 10;
            a = a + (a = 5);
            console.log(a);

            // a = 10?





            It's 15 because the value of a was 10 and you added (a = 5) after, which changed a's value to 5. Therefore, you did 10+5.






            var a = 10;
            a = (a = 5) + a;
            console.log(a);

            // a = 15?





            Now that you have (a = 5) first, you're changing its value to 5 before adding a. Therefore, you're doing 5+5.



            Hope this helps.






            share|improve this answer























            • But the expression in parentheses must be evaluated first, i thought that what operator precedence mean

              – Nikol Lakin
              Nov 14 '18 at 11:16
















            0

















            var a = 10;
            a = a + (a = 5);
            console.log(a);

            // a = 10?





            It's 15 because the value of a was 10 and you added (a = 5) after, which changed a's value to 5. Therefore, you did 10+5.






            var a = 10;
            a = (a = 5) + a;
            console.log(a);

            // a = 15?





            Now that you have (a = 5) first, you're changing its value to 5 before adding a. Therefore, you're doing 5+5.



            Hope this helps.






            share|improve this answer























            • But the expression in parentheses must be evaluated first, i thought that what operator precedence mean

              – Nikol Lakin
              Nov 14 '18 at 11:16














            0












            0








            0










            var a = 10;
            a = a + (a = 5);
            console.log(a);

            // a = 10?





            It's 15 because the value of a was 10 and you added (a = 5) after, which changed a's value to 5. Therefore, you did 10+5.






            var a = 10;
            a = (a = 5) + a;
            console.log(a);

            // a = 15?





            Now that you have (a = 5) first, you're changing its value to 5 before adding a. Therefore, you're doing 5+5.



            Hope this helps.






            share|improve this answer
















            var a = 10;
            a = a + (a = 5);
            console.log(a);

            // a = 10?





            It's 15 because the value of a was 10 and you added (a = 5) after, which changed a's value to 5. Therefore, you did 10+5.






            var a = 10;
            a = (a = 5) + a;
            console.log(a);

            // a = 15?





            Now that you have (a = 5) first, you're changing its value to 5 before adding a. Therefore, you're doing 5+5.



            Hope this helps.






            var a = 10;
            a = a + (a = 5);
            console.log(a);

            // a = 10?





            var a = 10;
            a = a + (a = 5);
            console.log(a);

            // a = 10?





            var a = 10;
            a = (a = 5) + a;
            console.log(a);

            // a = 15?





            var a = 10;
            a = (a = 5) + a;
            console.log(a);

            // a = 15?






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 10:47









            Ryan WallsRyan Walls

            6315




            6315












            • But the expression in parentheses must be evaluated first, i thought that what operator precedence mean

              – Nikol Lakin
              Nov 14 '18 at 11:16


















            • But the expression in parentheses must be evaluated first, i thought that what operator precedence mean

              – Nikol Lakin
              Nov 14 '18 at 11:16

















            But the expression in parentheses must be evaluated first, i thought that what operator precedence mean

            – Nikol Lakin
            Nov 14 '18 at 11:16






            But the expression in parentheses must be evaluated first, i thought that what operator precedence mean

            – Nikol Lakin
            Nov 14 '18 at 11:16


















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297856%2fjavascript-strange-behavior-with-operator-precedence1%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3