Objective
This article summarizes seven ways to exchange A and B variable values
var a = 123;
var B = 456;
Exchange Variable Value scheme one
The easiest way to do this is to use a temporary variable, but it's too low to use a temporary variable.
var T;
t = A;
A = b;
b = t;
First, the value of a is stored in a temporary variable, then B is assigned to a, and finally takes out a value in the temporary variable assigned to B, this method is the most basic
Exchange Variable Value Scheme II
The following scenario will not have a temporary variable, I summed up, in fact, the idea of not using temporary variables is to make one of the variables A and B have a relationship between the values, so you can change the value of another variable, and finally change the original modified value of the variable
Like this.
Let a ' and ' of A and B (also be replaced by A and B's difference, the same), ' and ' minus B cleverly got a variable value assigned to B, and then by ' and ' minus A's worth to B's value to give a, or the following variant (in the form of difference)
A-= B;
b = a + B;
A = b-a;
But feelings and forms are easier to understand.
Exchange Variable Value Scheme three
This method for the first time to learn JavaScript may not understand the students, because we rarely use JavaScript in place, this is what I learned in the previous algorithm competition book, through the low-level operations to exchange variable values, but also the above scheme evolved
You know, C + + can even a^=b^=a^=b
get the job done, but I find that JavaScript can't be
But we can do this.
Exchange Variable Value Scheme four
The first becomes an object, which holds the key value pairs that should be exchanged, and the final assignment is done.
A = {a:b,b:a};
b = a.b;
A = A.A;
Exchange Variable Value Scheme five
It's similar to the method above, except that the object is replaced with an array
A = [a,b];
b = a[0];
A = a[1];
Exchange Variable Value Scheme six
This method is very ingenious, not I want to come out, think out of the person must be the great God, unless he is a dream come out, simple and rough line of code exchange A and B variable values
Based on the operator precedence, the first is executed b=a
, at which point B directly gets the variable value of a, and then the one-step array index gives a the value of B (it can't be any worse).
Exchange Variable Value Scheme VII
Finally, my solution is to use the ES6 assignment syntax, which allows us to extract the values of arrays and objects, and assign values to variables, but the Chrome browser I'm testing now has implemented
It can be seen that the deconstruction assignment syntax makes our exchange variable values super simple, and this deconstruction assignment syntax, if you say a lot, is not the focus of today, it will be summed up now.
Summarize
This article mentioned so many ways to exchange variable values, do not know whether there are other ways, although it is a trivial matter, but can practice our brain hole. I hope the content of this article for everyone's study or work can bring certain help, if there is doubt you can message exchange.