swap two numbers without using third variable in javascript code example

Example 1: swap two variables javascript

var a = 1,
    b = 2;
b = [a, a = b][0];

Example 2: javascript swap two variables

[a, b] = [b, a];

Example 3: swap 2 integers without using temporary variable

using System;

class MainClass {
  public static void Main (string[] args) {
    int num1 = int.Parse(Console.ReadLine());
    int num2 = int.Parse(Console.ReadLine());
      num1 = num1 + num2; 
      num2 = num1 - num2; 
      num1 = num1 - num2; 
      Console.WriteLine("After swapping: num1 = "+ num1 + ", num2 = " + num2); 
    Console.ReadLine();
  }
}

Example 4: swap numbers in javascript

let a = 1;
let b = 2;
let temp;

temp = a;a = b;b = temp;
a; // => 2
b; // => 1

Tags:

C Example