首页 > 代码库 > exchangeVar() JavaScript交换两个变量的值

exchangeVar() JavaScript交换两个变量的值

function exchangeVar() {     var a = 1, b = 10    var temp = a;    a = b;    b = temp;    console.log(a + ‘\t‘ + b); // 10    1

不使用第三个变量的方法

function exchangeVar() {var a = 2, b = 20a = a + b;b = a - b;a = a - b;console.log(a + ‘\t‘ + b); // 20 2}

 

exchangeVar() JavaScript交换两个变量的值