SoFunction
Updated on 2025-04-29

Summary of JavaScript's method of exchanging two variable values ​​without using temporary variables

Method 1: Use arithmetic operations (addition and subtraction)

let a = 5;
let b = 10;

// Exchange processa = a + b;  // a = 15 (5 + 10)
b = a - b;  // b = 5 (15 - 10)
a = a - b;  // a = 10 (15 - 5)

(a); // Output: 10(b); // Output: 5

principle

  1. First store the sum of two numbers in the first variable
  2. Then subtract the second variable with the sum to get the value of the original first variable and assign it to the second variable
  3. Finally, subtract the new second variable (i.e. the original first variable) with the sum, obtain the value of the original second variable, and assign it to the first variable

Things to note

  • This method only works on numbers
  • When the value is large, there may be a risk of overflow

Method 2: Use arithmetic operation (multiple and division)

let a = 5;
let b = 10;

// Exchange processa = a * b;  // a = 50 (5 * 10)
b = a / b;  // b = 5 (50 / 10)
a = a / b;  // a = 10 (50 / 5)

(a); // Output: 10(b); // Output: 5

principle
Similar to addition and subtraction, but using multiplication and division

Things to note

  • Applicable to numbers only
  • There cannot be a value of 0, otherwise an error of dividing by 0 will occur.
  • There may be floating point accuracy issues

Method 3: Use bit operation (XOR exchange algorithm)

let a = 5;  // Binary 0101let b = 10; // Binary 1010
// Exchange processa = a ^ b;  // a = 15 (0101 ^ 1010 = 1111)
b = a ^ b;  // b = 5 (1111 ^ 1010 = 0101)
a = a ^ b;  // a = 10 (1111 ^ 0101 = 1010)

(a); // Output: 10(b); // Output: 5

principle

The properties of using XOR operations:

  1. Any number XOR itself result is 0: x ^ x = 0
  2. Any number XOR 0 result is itself: x ^ 0 = x
  3. ExoOR operation satisfies exchange law and bond law

advantage

  • Performance is usually better than arithmetic operations
  • Will not overflow

limit

  • Applicable to integers only
  • Poor code readability

Method 4: Use array deconstruction assignment (ES6+)

let a = 5;
let b = 10;

// Exchange process[a, b] = [b, a];

(a); // Output: 10(b); // Output: 5

principle
Using the deconstructed assignment feature of ES6, create a temporary array (processed internally by the engine, there are no explicit temporary variables in the code)

advantage

  • Concise and clear
  • Applicable to any type of value
  • Highly readable

Things to note

  • Requires ES6+ environment support
  • In fact, temporary variables may be used inside the engine, but from the code level, it is not explicitly used.

Method 5: Use object properties

let a = 5;
let b = 10;

// Exchange processa = {a: b, b: a};
b = ;
a = ;

(a); // Output: 10(b); // Output: 5

principle

Create a temporary object to store the exchanged values

Things to note

  • The code is a bit complicated
  • Suitable for all types

Method 6: Use comma operator

let a = 5;
let b = 10;

// Exchange processa = [b, b = a][0];

(a); // Output: 10(b); // Output: 5

principle

Take advantage of the characteristics of array index and comma operators

Things to note

  • Poor code readability
  • Suitable for all types

Method 7: Use function parameters

let a = 5;
let b = 10;

// Exchange processfunction swap(x, y) {
    return [y, x];
}
[a, b] = swap(a, b);

(a); // Output: 10(b); // Output: 5

advantage

  • Reusable
  • Clear and clear

Comparison of various methods

method Applicable Type readability performance Things to note
Addition and subtraction number medium generally Possible overflow
Multiplication and division Non-zero numbers medium generally There cannot be 0, accuracy problem
Bit operation Integer Low high Applicable to integers only
Deconstruction assignment Any high high Requires ES6+
Object properties Any medium generally -
Comma operator Any Low generally -
Function parameters Any high generally -

Practical application suggestions

  1. Modern development: Use deconstruction assignment first[a, b] = [b, a], simple and efficient, with good readability
  2. Need to be compatible with old environments: Use addition and subtraction or bit operations (selected according to the data type)
  3. Code Contest/Special Scene: Bit operation can be considered for best performance
  4. Actual engineering: Unless there are special needs, the traditional method of using temporary variables is actually very good, and readability is the most important thing

Special circumstances handling

Exchange object properties

const obj = {x: 5, y: 10};
[, ] = [, ];
(obj); // {x: 10, y: 5}

Swap array elements

const arr = [1, 2, 3];
[arr[0], arr[2]] = [arr[2], arr[0]];
(arr); // [3, 2, 1]

Summarize

While it is an interesting programming trick to not use temporary variables to exchange two values, in actual development, the readability and maintainability of the code are often more important than tiny performance optimizations. ES6's deconstruction assignment syntax provides the most elegant solution and is recommended for use in modern JavaScript development. For environments that do not support ES6, arithmetic or bit operation methods can be selected according to the specific situation.

The above is the detailed summary of the method of JavaScript that does not use temporary variables to exchange two variable values. For more information about JavaScript exchanging two variable values, please pay attention to my other related articles!