Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of exchanging values in JavaScript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What are the methods of exchanging values in JavaScript? I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

In the early days, when exchanging values in JS, we mainly used temporary variables. After ES6, we can use expansion operation symbols to exchange variables. What else can you think of besides these two ways?

1. Use temporary variables

Function swapWithTemp (num1, num2) {console.log (num1, num2) let temp = num1 num1 = num2 num2 = temp console.log (num1, num2)} swapWithTemp (66.66,8.88)

two。 Use the arithmetic operators + and-

Function swapWithPlusMinus (num1, num2) {console.log (num1, num2) num1num1 = num1 + num2 num2 = num1-num2 num1num1 = num1-num2 console.log (num1, num2)} swapWithPlusMinus (66, 8)

The main process is like this, first find the sum of two numbers, then the value of the first number is the total sum minus the second, that is, num2 = num1-num2 in the code. Similarly, the first number should be replaced with the value of the second number, that is, the total sum minus the value of the first number. Now the first number is assigned to the second number, so you can directly subtract the value of the second number, that is, num1 = num1-num2.

But I tried the decimal, there seems to be a problem, a little awkward, but we still have to master this kind of thinking.

It can also be abbreviated as follows:

Function swapWithPlusMinusShort (num1, num2) {console.log (num1, num2) num2 = num1 + (num1 = num2)-num2 console.log (num1, num2)}

The trick here is (num1 = num2), where we make num1 equal to num2 and return the value of num2, where the num1 value has been swapped. Then use num1 plus the value returned by (num1 = num2), that is, num1 + num2, and the train of thought is the same as the above analysis.

However, when using floating point numbers, you can get some unexpected results.

You can run the following code in the console:

Function swapWithPlusMinusShort (num1, num2) {console.log (num1, num2) num2 = num1 + (num1 = num2)-num2 console.log (num1, num2)} swapWithPlusMinusShort

3. Use only the + or-operator

As long as you use the + operator, you can get the same result as using both + and -.

Function swapWithPlus (num1, num2) {console.log (num1, num2) num2 = num1 + (num1=num2, 0) console.log (num1, num2)} swapWithPlus

The above program works at the expense of readability. In (), we assign num1 to num2, and the 0 next to it is the return value. In short, line 4 looks like this:

Num2 = num1 + 0 = > num2 = num1

4. Use the arithmetic operators * and /

The principle of * and / is the same as the previous method, except that there are some slight differences.

Function swapWithMulDiv (num1, num2) {console.log (num1, num2) num1num1 = num1*num2 num2 = num1/num2 num1num1 = num1/num2 console.log (num1, num2)} swapWithMulDiv (2.3)

Same as the previous one. We take the product of two numbers and store them in one of the variables, corresponding to num1 = num1*num2. Then, the value of the swapped variable is obtained by using the total in addition to the corresponding variable.

But what are some of the problems? That is, if there is an exchange value of 0, you will get an unexpected problem:

SwapWithMulDiv (2.34) / / 2.340 / / NaN NaN

Instead of swapping our values, we get a strange NaN. What was that all about? If you remember your math class, we are always told not to divide by 0 because it is undefined. The reason is how the limit works, and there are other reasons that we won't cover. Now, let's look at other problems with this approach:

Function swapWithMulDiv (num1, num2) {console.log (num1, num2) num1num1 = num1*num2 num2 = num1/num2 num1num1 = num1/num2 console.log (num1, num2)} swapWithMulDiv (2.34 Infinity / / NaN NaN)

NaN again, because we can't divide Infinity by task content, so ⚡ is not defined.

What if it is negative infinity? what will be the result?

Function swapWithMulDiv (num1, num2) {console.log (num1, num2) num1num1 = num1*num2 num2 = num1/num2 num1num1 = num1/num2 console.log (num1, num2)} swapWithMulDiv (2.34)

The result of-Infinity is the same as the previous example, and for the same reason.

The following is an abbreviation above, and of course the problem is the same:

Function swapWithMulDivShort (num1, num2) {console.log (num1, num2) num1num1 = num1*num2 num2 = num1* (num1=num2) / num2 num1num1 = num1/num2 console.log (num1, num2)} swapWithMulDivShort

5. Use only the * or / operators

The above program works at the expense of readability. In (), we assign num1 to num2, and the 1`` next to it is the return value. Num2 = num1 * (num1=num2, 1) looks like this:

Num2 = num1 * 1 = > num2 = num1

6. Use bitwise XOR

XOR works in binary bits, and when we have two different values, the result is 1, otherwise it is 0:

Function swapWithXOR (num1, num2) {console.log (num1, num2) num1num1 = Num1 ^ num2; num2 = Num1 ^ num2; num1num1 = Num1 ^ num2; console.log (num1, num2)} swapWithXOR (10Jie 1)

4-digit binary number 10-> 1010

4-digit binary number 1-> 0001

The above decomposition process:

Num1num1 = num1 ^ num2 = 1010 ^ 0001 = 1011 num2 = num1 ^ num2 = 1011 ^ 0001 = > 1010 = > 10 num1num1 = num1 ^ num2 = 1011 ^ 1010 = > 0001 = > 1

Let's look at another example.

Function swapWithXOR (num1, num2) {console.log (num1, num2) num1num1 = num1 ^ num2; num2 = num1 ^ num2; num1num1 = num1 ^ num2; console.log (num1, num2)} swapWithXOR (2.34 3.45) / / 2.343.45 / / 32

Yeah? Where is the value of the exchange? We only get the integer part of this number. That's the problem. XOR assumes that the input is an integer, so the corresponding calculation is performed. But floating-point numbers are not integers and are represented by the IEEE 754 standard, which divides numbers into three parts: a symbolic bit, a set of bits representing exponents, and another set of Mantissa numbers between 1 (inclusive) and 2 (excluding), so we get incorrect values.

Another example:

Function swapWithXOR (num1, num2) {console.log (num1, num2) num1num1 = Num1 ^ num2; num2 = Num1 ^ num2; num1num1 = Num1 ^ num2; console.log (num1, num2)} swapWithXOR (- Infinity,Infinity) / /-Infinity Infinity / / 0 0

Once again, we did not see the expected results. This is because both Infinity and-Infinity are floating-point numbers. As we discussed above, floating-point numbers are a problem for XOR.

Use the same or gate XNOR.

The same or gate, also known as an exclusive or non gate, can also operate binary bits, as opposed to XOR. When we have two different values, the XNOR result is 0, otherwise it is 1. JavaScript does not have an operator to execute XNOR, so we use a non-XOR operator to achieve a similar effect.

Function swapWithXNOR (num1, num2) {console.log (num1, num2) num1 = ~ (num1 ^ num2) num2 = ~ (num1 ^ num2) num1 = ~ (num1 ^ num2) console.log (num1, num2)} swapWithXNOR

4-digit binary number 10-> 1010

4-digit binary number 1-> 0001

The above decomposition process:

Num1 = ~ (num1 ^ num2) > ~ (1010 ^ 1011) = > ~ (1011) = > ~ 11 = >-12

Since we have a negative number, we need to convert it back to binary and execute the complement of 2 to get the decimal value, for example:

-12 = > 1100 = > 0011 + 1 = > 0100num2 = ~ (num1 ^ num2) = > ~ (0100 ^ 0001) = > ~ (0101) = > ~ 5 = >-6-6 = > 0110 = > 1001 + 1 = > 1010 = > 10num1 = ~ (num1 ^ num2) = > ~ (0100 ^ 1010) = > ~ (1110) = > ~ 14 = >-15-15 = > 1111 = > 0000 + 1 = > 0001 = > 1

It took some time, but we exchanged value. Unfortunately, it has the same problem as XOR, it can't handle floating-point numbers and infinity.

Function swapWithXNOR (num1, num2) {console.log (num1, num2) num1 = ~ (num1 ^ num2) num2 = ~ (num1 ^ num2) num1 = ~ (num1 ^ num2) console.log (num1, num2)} swapWithXNOR (2.3 4.5) / / 2.34.5 / 42

8. Assign values in an array

This is a simple technique that requires only one line to perform the exchange and, more importantly, no mathematical knowledge, just a basic array knowledge.

Function swapWithArray (num1, num2) {console.log (num1, num2) num2 = [num1, num1 = num2] [0] console.log (num1, num2)} swapWithArray (2.3 Infinity// Infinity 2.3)

In index 0 of the array, we store num1, and in index 1, we assign num2 to num1 and store num2. In addition, access [0] to store the num1 values in the array in num2.

This way we can exchange anything we want, including integers, floating-point numbers (including infinity), and strings. It's neat, but not clear enough.

9. Use deconstructing expressions

This is a feature of ES6, and it is the simplest, and we can exchange values like this:

Let num1 = 23.45 let num2 = 45.67 console.log (num1,num2) [num1,num2] = [num2,num1] console.log (num1,num2)

10. Use immediately called function expressions (IIFE)

IIFE refers to a function that is executed immediately after it is defined.

Function swapWithIIFE (num1,num2) {console.log (num1,num2) num1 = (function (num2) {return num2;}) (num2, num2=num1) console.log (num1,num2)} swapWithIIFE

In the above example, we immediately call a function on line 4. The last parentheses are the arguments to the function. The second parameter assigns num1 to num2, and the first parameter, num1, is returned. Therefore, after exchanging these values, keep in mind that this exchange method is not efficient.

After reading the above, have you mastered the methods of exchanging values in JavaScript? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report