+ 1

How on Earth does ( 4-- ) give you 4 , I thought 4-- means that 1 must be subtracted from 4 ?

2nd Jul 2021, 9:09 PM
Theresco
Theresco - avatar
4 Antworten
+ 4
First, the increment and decrement operators do not work with numbers, they apply to variables, for example: var a = 0, b = 10; a = b--; console.log('a =', a) // a = 10 console.log('b = ', b) // b = 9 It's very easy to understand: a = b-- (first assigns, then subtracts) a = --b (first subtracts, then assigns) If you immediately draw the output of the operand, then there will be the same sequence, for example: console.log('a =', a--) // a = 10 console.log('a =', a) // a = 9 (output first, then subtraction)
2nd Jul 2021, 10:50 PM
Solo
Solo - avatar
+ 2
Theresco Yes 1 will be subtract but after assigning value. var a = 4; var b = a--; //here a-- will be assign to b then a will be subtract by 1 console.log(b); //4 console.log(a); //3
3rd Jul 2021, 1:46 AM
A͢J
A͢J - avatar
+ 2
Thank you everyone 🙃
3rd Jul 2021, 3:45 AM
Theresco
Theresco - avatar
+ 1
it's a postfix, it subtracts one after the loop or whatever block ends. make it --4 and it'll do what you're thinking
2nd Jul 2021, 9:19 PM
Slick
Slick - avatar