当前位置:网站首页>Detailed analysis of operators i++ and ++i in JS, i++ and ++i

Detailed analysis of operators i++ and ++i in JS, i++ and ++i

2022-07-01 00:58:00 imkaifan

let i = 1,o = 1;
let k = i++;
let j = ++o;

console.log(k,i)  //1 2 ( after ++) First assign yourself to k, And then I'll do it myself ++
console.log(j,o)  //2 2 ( First ++) First execute yourself ++ operation , Then assign the result to j

i++ It returns the value before autoincrement ,++i The returned value is the self incremented value i++ Good ++i In fact, it is an expression , An expression always returns a value

let a = 10,b = 20,c = 30;
++a; // 11 ++a You mean for a=a+1 a by 11, At this time, the operation priority is not involved .
a++; // 12 a++ You mean for a=a+1 a by 12 , At this time, the problem of operation priority is still not involved .

e = ++a + ++b + c++ + a++;  // 13 + 21 + 30 + 13 = 77( His writing , It belongs to a mistake )
//  Correct explanation 
//  At this point, we must consider the problem of operation level 
// ++a a First of all +1  Then assign the value to the result :13 a=13
// ++b b First of all +1  Then assign the value to the result :21 b=21
// c++  First the c Value is assigned to the result :30 c own +1 c=31
// a++  First the a value 13 Assign results :13 a own +1 a=14

console.log(e)  // 77
原网站

版权声明
本文为[imkaifan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160413039521.html