Interview: How to convert the value between two variables in the fastest and easiest way?

problem: As shown below, now there are two variables, a and b. How can a get the value of b and b get the value of a in the easiest way?

> let a = 1
> let b = 2

The fastest and easiest way is to use: ES6 deconstruction allocation in JavaScript

 let a = 1
 let b = 2
 [b, a] = [a, b]
 console.log(a,b) // 2 1

In this way, this problem is solved.

Guess you like

Origin blog.csdn.net/m0_46442996/article/details/112695380