Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Program to Swap First and Last Elements in an Array

JavaScript Program to Swap First and Last Elements in an Array

In this article, we are going to learn about swaping the first and last elements in an array in JavaScript. Swaping the first and last elements in a JavaScript array refers to swapping their positions, effectively exchanging the values at these two distinct indices within the array.

There are several methods that can be used to interchange the first and last elements in an array in JavaScript, which are listed below:

  • Using Temporary Variable
  • Array destructuring
  • Using XOR Bitwise operator
  • Using splice() method

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using Temporary Variable

Interchanging first and last array elements in JavaScript involves storing the first element in a temporary variable, and then swapping values between the first and last elements.

Syntax:

let temp = arr1[0];
arr1[0] = arr1[arr1.length - 1];
arr1[arr1.length - 1] = temp;

Example: In this example, we are using a temp variable to interchange 1st index value with the last index value of our array.

Javascript




let arr1 = [10, 20, 30, 40, 50];
let temp = arr1[0];
arr1[0] = arr1[arr1.length - 1];
arr1[arr1.length - 1] = temp;
console.log("Array after interchange:", arr1);


Output

Array after interchange: [ 50, 20, 30, 40, 10 ]

Approach 2: Array Destructuring

In this approach we are using Array destructuring assigns values from an array to variables, swapping the first and last elements.

Syntax:

[array[0], array[array.length - 1]] = [array[array.length - 1], array[0]];

Example:

Javascript




let array = [10, 2, 5, 12, 7];
[array[0], array[array.length - 1]] = [array[array.length - 1], array[0]];
console.log("Array after swapping : " + array);


Output

Array after swapping : 7,2,5,12,10

Approach 3: Using XOR Bitwise Operator

In this approach, the XOR (^) bitwise operator to interchange the values of the first and last elements in the array without using a temporary variable.

Syntax:

array[0] = array[0] ^ array[array.length - 1];
array[array.length - 1] = array[0] ^ array[array.length - 1];
array[0] = array[0] ^ array[array.length - 1];

Example: In this example we are using XOR operator to interchange the element in your given array.

Javascript




const array = [1, 2, 3, 4, 5];
array[0] = array[0] ^ array[array.length - 1];
array[array.length - 1] = array[0] ^ array[array.length - 1];
array[0] = array[0] ^ array[array.length - 1];
console.log("Array after interchange:", array);


Output

Array after interchange: [ 5, 2, 3, 4, 1 ]

Approach 4: Using splice() Method

In this approach, splice() method is used to replace the last element with the first element, and the result is assigned back to the first position in the array. This effectively interchanges the values 12 and 55 in the array.

Syntax:

Array.splice( index, remove_count, item_list )

Example: In this example we are using the above-explained approach.

Javascript




let myArray = [12, 22, 33, 44, 55];
myArray[0] = myArray.splice(myArray.length - 1, 1, myArray[0])[0];
console.log(myArray);


Output

[ 55, 22, 33, 44, 12 ]

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments