0%

+=和=+的区别

=+和+=的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
let a = 100;

a += 1;
console.log("a1=" + a); //a1 = 101

a -= 1;
console.log("a2=" + a); //a2 = 99

a = +1;
console.log("a3=" + a); //a3 = 1

a = -1;
console.log("a4=" + a); //a4 = -1
  • += 是简写,a += 1 就是 a = a+1
  • =+并不是简写,a =+ a 直接对 a 的赋值,± 符号代表的是正负(完全可以省略不写),即 a =+ b 其实就是 a = b

a++和++a的区别

1
2
3
let a= 1;
console.log(++a); //2,先执行a=a+1,再输出a
console.log(a++); //1,先输出a,在执行a=a+1