Javascript之两个变量值的交换

  • ES5:
var a = 12,b=13,c;
c = a;
a = b;
b = c;
console.log(a,b);//13,12

  

通过设置第三方变量交换赋值来实现
 
  • ES6
var a = 12,b=13;
[a,b] = [b,a];
console.log(a,b);
采用ES6数组结构的方式实现
 

猜你喜欢

转载自www.cnblogs.com/bgwhite/p/9335706.html