# JavaScript 交换值的奇思妙想

## 1. 使用临时变量

function swapWithTemp(num1, num2) {
console.log(num1, num2)

let temp = num1
num1 = num2
num2 = temp

console.log(num1, num2)
}

swapWithTemp(66.66, 8.88)

## 2. 使用算术运算符+和-

function swapWithPlusMinus(num1, num2){
console.log(num1, num2)

num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2

console.log(num1, num2)
}

swapWithPlusMinus(66, 8)

function swapWithPlusMinusShort(num1, num2){
console.log(num1, num2)

num2 = num1 + (num1 = num2) - num2

console.log(num1, num2)
}

function swapWithPlusMinusShort(num1, num2){
console.log(num1, num2)

num2 = num1 + (num1 = num2) - num2

console.log(num1, num2)
}

swapWithPlusMinusShort(2,3.1)

## 3.仅使用+或-运算符

function swapWithPlus(num1, num2){
console.log(num1, num2)

num2 = num1 + (num1=num2, 0)

console.log(num1, num2)
}

swapWithPlus(2.3,3.4)

num2 = num1 + 0 => num2 = num1

## 4. 使用算术运算符*和/

*/的原理与先前的方法相同，只是有一些微小的区别 😎。

function swapWithMulDiv(num1, num2){
console.log(num1, num2)

num1 = num1*num2
num2 = num1/num2
num1 = num1/num2

console.log(num1, num2)
}

swapWithMulDiv(2.3,3.4)

swapWithMulDiv(2.34,0)
// 2.34 0
// NaN NaN

function swapWithMulDiv(num1, num2){
console.log(num1, num2)

num1 = num1*num2
num2 = num1/num2
num1 = num1/num2

console.log(num1, num2)
}

swapWithMulDiv(2.34,Infinity)
// 2.34 Infinity
// NaN NaN

function swapWithMulDiv(num1, num2){
console.log(num1, num2)

num1 = num1*num2
num2 = num1/num2
num1 = num1/num2

console.log(num1, num2)
}

swapWithMulDiv(2.34,-Infinity)

-Infinity的结果与前面的示例相同，原因也是一样的。

function swapWithMulDivShort(num1, num2){
console.log(num1, num2)

num1 = num1*num2
num2 = num1*(num1=num2)/num2
num1 = num1/num2

console.log(num1, num2)
}

swapWithMulDivShort(2.3,3.4)

## 5. 仅使用*或/运算符

num2 = num1 * 1 => num2 = num1

## 6. 使用按位异或

function swapWithXOR(num1, num2){
console.log(num1, num2)

num1 = num1^num2;
num2 = num1^num2;
num1 = num1^num2;

console.log(num1, num2)
}

swapWithXOR(10,1)

4位二进制数10-> 1010

4位二进制数1-> 0001

num1 = num1 ^ num2 = 1010 ^ 0001 = 1011
num2 = num1 ^ num2 = 1011 ^ 0001 => 1010 => 10
num1 = num1 ^ num2 = 1011 ^ 1010 => 0001 => 1

function swapWithXOR(num1, num2){
console.log(num1, num2)

num1 = num1^num2;
num2 = num1^num2;
num1 = num1^num2;

console.log(num1, num2)
}

swapWithXOR(2.34,3.45)
// 2.34 3.45
// 3 2

function swapWithXOR(num1, num2){
console.log(num1, num2)

num1 = num1^num2;
num2 = num1^num2;
num1 = num1^num2;

console.log(num1, num2)
}

swapWithXOR(-Infinity,Infinity)
// -Infinity Infinity
// 0 0

1. 使用同或门 XNOR

function swapWithXNOR(num1, num2){
console.log(num1, num2)

num1 = ~(num1^num2)
num2 = ~(num1^num2)
num1 = ~(num1^num2)

console.log(num1, num2)
}

swapWithXNOR(10,1)

4位二进制数10-> 1010

4位二进制数1-> 0001

num1 = ~(num1 ^ num2) => ~(1010 ^ 1011)=> ~(1011) => ~11 => -12

-12 => 1100 => 0011 + 1 => 0100
num2 = ~(num1 ^ num2) => ~(0100 ^ 0001) => ~(0101) => ~5 => -6

-6 => 0110 => 1001 + 1 => 1010 => 10
num1 = ~(num1 ^ num2) => ~(0100^ 1010) => ~(1110) => ~14 => -15

-15 => 1111 => 0000 + 1 => 0001 => 1

function swapWithXNOR(num1, num2){
console.log(num1, num2)

num1 = ~(num1^num2)
num2 = ~(num1^num2)
num1 = ~(num1^num2)

console.log(num1, num2)
}

swapWithXNOR(2.3,4.5)
// 2.3 4.5
// 4 2

## 8. 在数组中赋值

function swapWithArray(num1, num2){
console.log(num1, num2)

num2 = [num1, num1 = num2][0]

console.log(num1, num2)
}

swapWithArray(2.3,Infinity)

// 2.3 Infinity
// Infinity 2.3

## 9. 使用解构表达式

let num1 = 23.45
let num2 = 45.67

console.log(num1,num2)

[num1,num2] = [num2,num1]

console.log(num1,num2)

## 10. 使用立即调用的函数表达式（IIFE）

IIFE指的是在定义后立即执行的函数。

function swapWithIIFE(num1,num2){
console.log(num1,num2)

num1 = (function (num2){ return num2; })(num2, num2=num1)

console.log(num1,num2)
}

swapWithIIFE(2.3,3.4)

40746 人关注
451 篇文章