Actually, this is the question: if you believe https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence , then the increment / decrement operators do not have the highest priority. But in practice, regardless of the form (postfix or prefix - no matter), they are executed earlier than the grouping operator or function call:

function test(i) { console.log(i); return(i); }; let i = 0; i++ + test(i); //1 

That is, the variable is incremented by 1 first, then the already increased value is passed to the function, and then, since the postfix increment form, when performing binary addition, its value is used before the increase, that is 0. By wrapping the function in brackets, nothing has changed.

It turned out that the increment was executed earlier than the function, although judging by the priority table, this should not be the case.

Or is it a feature of the interaction of unary and binary operators (in this case, a plus that runs from left to right)? I climbed a bunch of forums - I could not find the answer. I hope someone can explain to me, preferably in simple words;)

  • Why should not? You need to push off from addition: first, the first / left operand ( i++ ) is calculated, then the second ( test(i) ). Then the very addition occurs. What are the priorities of the operators used inside the operands - it does not matter. - Regent
  • The comparison always goes between two operands, in your example two comparisons occur, first ++ and +, the first is chosen, since it has a higher priority, then there is a comparison + and a function call, the second one is selected, since higher priority, and the last moment is the addition. - user218976
  • Now it is clear, you still need to build on the associativity of binary operators. - halfPeiceOfGangsta
  • It’s even probably more accurate to say that a unary operator inside an expression will always be executed before a binary one, but it will not preserve the order with other unary operators if they are inside different operands of binary operators. - halfPeiceOfGangsta
  • @halfPeiceOfGangsta yes. Since addition has lower priority than increment and function call, increment and function call are addition operands, which by associativity are calculated from left to right, without taking into account priorities among themselves. - Regent

0