The Array Way to Swap Variable - js,py and etc. i think.

mmvergara - Aug 23 '22 - - Dev Community

This will pretty much will work in any data types im assuming?

let a = 'a1'
let b = 'b1'

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

console.log(a,b) // b1,a1
Enter fullscreen mode Exit fullscreen mode
a = 'a1'
b = 'b1'

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

print(a,b) # b1,a1
Enter fullscreen mode Exit fullscreen mode

There are better ways like, destructuring, this is just an alternative

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .