Seven solutions for JavaScript to exchange values of two variables without a temporary variable

Source: Internet
Author: User

Objective

This article summarizes seven ways to exchange values for variables A and b

var a = 123;var B = 456;

Exchange Variable Value scheme one

The simplest way to do this 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, the value of a is stored in a temporary variable, then B is assigned to a, and finally the value of a in the temporary variable assigned to B, this method is the most basic

Exchange Variable Value Scheme II

The following scenarios do not have temporary variables, I summed up, in fact, the idea of not using temporary variables is to let one of the variables into a and B are related to the value, so you can change the value of another variable, and finally change the original modified variable value

Like this one.

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

Let a first become a and B's ' and ' (can also be changed to A and B, the same), ' and ' minus B cleverly obtained a variable value assigned to B, and then by ' and ' minus A is worth the value of B given a, or the following variant (the form of difference)

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

But feelings and forms are easier to understand.

Exchange Variable Value Scheme III

This method for the first time to learn JavaScript may not understand, because our JavaScript is rarely used in place, this is what I learned before the algorithm contest book, through the underlying bit operation to exchange variable values, is the above scheme evolved

A ^= b;//or b ^= a;a ^= b;

Know that C + + can even a^=b^=a^=b do the job, but I find that JavaScript can't

But we can do this.

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

Exchange Variable Value Scheme four

Turn A into an object, this object holds the key value pairs that should be exchanged, and the last assignment is done.

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

Exchange Variable Value Scheme five

Much like the method above, except that the object was replaced with an array

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

Exchange Variable Value Scheme VI

This method is very ingenious, not I thought out, the person who thinks out must be the great God, unless he is a dream come out, simple rude line of code exchanged a and B variable values

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

According to the operator precedence, the first execution b=a , at this time B directly get a variable value, and then a step array index let a get the value of B (can not be more powerful)

Exchange Variable Value Scheme VII

Finally, my solution is to take advantage of ES6 's deconstructed assignment syntax, which allows us to extract the values of arrays and objects, and assign values to variables, but I now have a test Chrome browser that has implemented

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

Seven solutions for JavaScript to exchange values of two variables without a temporary variable

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.