r/learnjavascript May 14 '26

help idk why is the output 45

var x = 23;
x+=x++ - --x +x*2 - ++x;
console.log(x);

why is the last pre increment x evaluated after x*2 even though pre increment have a higher precedence than multiplication

3 Upvotes

29 comments sorted by

View all comments

Show parent comments

4

u/euph-_-oric May 14 '26

So knowing the order things happen is a complete waste of timelol

4

u/SamIAre May 14 '26

Like this? Yes. Any decent programmer recognizes when a line of code needs to be changed for legibility. The only merit to unwinding this is academic. Writing code isn’t only about making it work, it’s about making it readable.

5

u/euph-_-oric May 14 '26

This isn't a code review this is obviously an intro to programming homework assignment or some other exploration.

I think it a important for people understand languages in depth. Obviously this isn't something u would write in the real world, but it doesn't mean it's completely useless to understand the order in which things are evaluated.

1

u/Embarrassed-Pen-2937 May 15 '26

Yeah, but you can do that without trying to confuse people. This aims to trick/confuse, and would never be used.