10. How to swap two variables in JavaScript

Swap two variables
  


        // Swip two number

        // Using third variable

        function swipNumbers(num1, num2) {

        var tempNumber = num1,
        num1 = num2;
        num2 = tempNumber;
        console.log(`num1 is : ${num1}`);
        console.log(`num1 is : ${num2}`);
        }

        swipNumbers(2, 6);

        //With mathematical operations


        function swipNumber1(a, b) {

        a = a + b
        b = a - b
        a = a - b

        console.log(`num1 is : ${a}`);
        console.log(`num1 is : ${b}`);
        }
        swipNumber1(2, 6);

        //using XOR


        function swipNumber2(a, b) {

        a = a ^ b
        b = a ^ b
        a = a ^ b
        console.log(`num1 is : ${a}`);
        console.log(`num1 is : ${b}`);
        }

        swipNumber2(2, 6);

        //Single line swapping with addition

        function swipNumber3(a, b) {

        a = b + (b = a, 0)
        console.log(`num1 is : ${a}`);
        console.log(`num1 is : ${b}`);
        }

        swipNumber3(2, 6);

        //Single line swapping with addition

        function swipNumber4(a, b) {


        a = b + (b = a, 0)
        console.log(`num1 is : ${a}`);
        console.log(`num1 is : ${b}`);
        }
        // 1. (b=a, 0) sets b to the old value of a and yields 0
        // 2. a = b + 0 sets a to the old value of b
        swipNumber4(2, 6);

        //Single line swapping with addition

        function swipNumber5(a, b) {


        b=a+(a=b)-b
        console.log(`num1 is : ${a}`);
        console.log(`num1 is : ${b}`);
        }

        swipNumber5(2, 6);


        //Classic one-line method

        function swipNumber6(a, b) {


        a = [b, b=a][0];
        console.log(`num1 is : ${a}`);
        console.log(`num1 is : ${b}`);
        }

        swipNumber6(2, 6);

        //Using ES6 self executing arrow functions


        function swipNumber7(a, b) {


        b = (a=>a)(a,a=b);
        console.log(`num1 is : ${a}`);
        console.log(`num1 is : ${b}`);
        }

        swipNumber7(2, 6);

        //ES6+ method


        function swipNumber8(a, b) {


        [a, b] = [b, a]
        console.log(`num1 is : ${a}`);
        console.log(`num1 is : ${b}`);
        }

        swipNumber8(2, 6);