JavaScript Program to Swap Two Variables

Introduction

Swapping two variables means exchanging their values. This is a common task in programming and can be achieved in several ways in JavaScript. This guide will walk you through writing a JavaScript program to swap two variables using different methods, such as using a temporary variable and swapping without a temporary variable.

Problem Statement

Create a JavaScript program that:

  • Accepts two variables.
  • Swaps their values.
  • Displays the values of the variables before and after swapping.

Example:

  • Input: a = 5, b = 10
  • Output:
    Before Swap: a = 5, b = 10
    After Swap: a = 10, b = 5
    

Solution Steps

  1. Initialize Two Variables: Define two variables with initial values.
  2. Swap the Values Using Different Methods:
    • Use a temporary variable.
    • Swap without a temporary variable using arithmetic operations.
    • Use array destructuring.
  3. Display the Result: Output the values before and after swapping.

JavaScript Program

Method 1: Using a Temporary Variable

// JavaScript Program to Swap Two Variables using a Temporary Variable
// Author: https://www.rameshfadatare.com/

function swapWithTemp(a, b) {
    console.log(`Before Swap: a = ${a}, b = ${b}`);

    // Step 1: Use a temporary variable to swap the values
    let temp = a;
    a = b;
    b = temp;

    console.log(`After Swap: a = ${a}, b = ${b}`);
}

// Example usage
swapWithTemp(5, 10);

Method 2: Without a Temporary Variable (Using Arithmetic)

// JavaScript Program to Swap Two Variables without a Temporary Variable using Arithmetic
// Author: https://www.rameshfadatare.com/

function swapWithoutTemp(a, b) {
    console.log(`Before Swap: a = ${a}, b = ${b}`);

    // Step 1: Swap using arithmetic operations
    a = a + b;
    b = a - b;
    a = a - b;

    console.log(`After Swap: a = ${a}, b = ${b}`);
}

// Example usage
swapWithoutTemp(5, 10);

Method 3: Using Array Destructuring (ES6)

// JavaScript Program to Swap Two Variables using Array Destructuring (ES6)
// Author: https://www.rameshfadatare.com/

function swapWithDestructuring(a, b) {
    console.log(`Before Swap: a = ${a}, b = ${b}`);

    // Step 1: Swap using array destructuring
    [a, b] = [b, a];

    console.log(`After Swap: a = ${a}, b = ${b}`);
}

// Example usage
swapWithDestructuring(5, 10);

Explanation

Method 1: Using a Temporary Variable

  • This method uses a third (temporary) variable to store the value of a, then assigns the value of b to a and finally assigns the temporary variable to b.

Method 2: Without a Temporary Variable (Using Arithmetic)

  • This method uses arithmetic operations to swap the values without needing a temporary variable:
    • a = a + b: The sum of a and b is stored in a.
    • b = a - b: b is updated to the original value of a.
    • a = a - b: a is updated to the original value of b.

Method 3: Using Array Destructuring (ES6)

  • ES6 introduced array destructuring, which allows swapping values in a single line by assigning them to an array and swapping positions.

Output Example

For all methods, the output is:

Before Swap: a = 5, b = 10
After Swap: a = 10, b = 5

Conclusion

This JavaScript program demonstrates three different methods to swap two variables: using a temporary variable, without a temporary variable (using arithmetic), and using array destructuring. All methods are efficient, but the array destructuring method introduced in ES6 is the cleanest and most concise solution. This exercise is valuable for understanding variable manipulation and exploring different techniques for swapping values in JavaScript.

Comments