+ 1
How on Earth does ( 4-- ) give you 4 , I thought 4-- means that 1 must be subtracted from 4 ?
4 ответов
+ 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)
+ 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
+ 2
Thank you everyone 🙃
+ 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