SoFunction
Updated on 2025-03-08

Seven solutions for exchanging two variable values ​​in JavaScript

Preface

This article summarizes seven ways to exchange variable values ​​of a and b

var a = 123;
var b = 456;

Exchange variable value scheme one

The easiest way is to use a temporary variable, but the method of using temporary variables is too low

var t;
t = a;
a = b;
b = t;

First, store the value of a into the temporary variable, then assign b to a, and finally take out the value of a in the temporary variable to be assigned to b. This method is the most basic

Scheme 2 of exchange variable value

The following schemes do not have temporary variables. I have summarized that the idea of ​​not using temporary variables is to make one of the variables a value that has a relationship with a and b, so that the value of the other variable can be changed first, and finally the original modified variable value is changed.

For example this

a += b;
b = a - b;
a -= b;

Let a first become the ‘sum’ of a and b (can also be replaced by the difference between a and b, the same), subtracting ‘and’ and ‘b cleverly obtain the variable value of a and is assigned to b, and then subtracting the value of a and the value of b by ‘and’ is given to a, or the following variation (the form of difference)

a -= b;
b = a + b;
a = b - a;

But the feeling and form is easier to understand

Scheme for exchange variable value three

This method may not be understood by students who are learning JavaScript for the first time, because we rarely use JavaScript operations. This is what I learned when I was reading algorithm competition books before. We exchange variable values ​​through underlying bit operations, which is also the evolution of the above solution

a ^= b;
b ^= a;
a ^= b;

Let's find out, C++ can evena^=b^=a^=bTo complete the task, but I found that JavaScript cannot

But we can do this

a = (b^=a^=b)^a;

Scheme 4 of exchange variable value

Turn a first into an object, which saves the key-value pairs that should be exchanged, and finally assigns the value to the end.

a = {a:b,b:a};
b = ;
a = ;

Scheme 5 for swapping variable values

It's very similar to the above method, except that the object is replaced with an array

a = [a,b];
b = a[0];
a = a[1];

Scheme for exchanging variable values ​​6

This method is very clever. It was not what I thought of. The person who thought of it must be a great god, unless he came up with a dream. He exchanged the variable values ​​of a and b in a simple and crude line of code.

a = [b,b=a][0];

According to operator priority, first executeb=a, at this time b directly gets the variable value of a, and then a step in the array index to get the value of b (it's simply not that great)

Scheme for exchanging variable values

Finally, my solution is to use ES6's deconstruction assignment syntax, which allows us to extract the values ​​of arrays and objects and assign variables. However, the Chrome browser I am testing now has implemented it.

[a,b] = [b,a];

You can see that the deconstruction assignment syntax makes our exchange variable values ​​super simple. If this deconstruction assignment syntax is said, it will be said a lot, which is not the focus of today. I will summarize it in the future and I will not talk about it now.

Summarize

This article mentions so many methods of exchanging variable values. I don’t know if there are any other methods. Although it is an irrelevant question, we can practice our brains. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.