JS implementation uses no intermediate variable temp to implement two variable value exchange methods, jstemp

Source: Internet
Author: User

JS implementation uses no intermediate variable temp to implement two variable value exchange methods, jstemp

1. Use addition and subtraction;

Var a = 1; var B = 2; a = a + B; B = a-B; a = a-B;

2. Use multiplication and division (multiplication and division are more like the ing from addition and subtraction to multiplication and division operations)

Var a = 1; var B = 2; a = a * B; B = a/B; a = a/B;

Note: This method can be used to exchange integer and floating-point values, but loss of precision may occur when processing floating-point values, and B cannot be 0 when multiplication and division are performed;

3. array flexibility

Var a = 1; var B = 2; a = [B, B = a] [0];

Summary

The above is a small series of JS implementations that don't use the intermediate variable temp to implement two variables worth exchanging methods. I hope to help you. If you have any questions, please leave a message for me, the editor will reply to you in time!

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.