JavaScript Exchange two variable values of seven solutions _javascript tips

Source: Internet
Author: User

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.

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

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

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

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.

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

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

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

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

[A,b] = [B,a];

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.