Swapping two array elements in a single line using JavaScript Last Updated : 11 Jun, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, there exist many ways by which one can swap two array elements. In this article, we will discuss a way in which one can swap two array elements in JavaScript in a single line. The input and output would be as follows. Input: arr = { 10, 20, 40, 30 }Output: arr = { 10, 20, 30, 40 } // Swapped 30 and 40Here are some common approaches: Table of Content Using Destructuring MethodUsing XOR Bitwise Operator Using Array.spliceUsing Destructuring MethodThis is a far better method than anyone. This method can be executed in a single line. This swapping can be done by writing the 2 array elements and want to reverse in order and in square brackets on the left-hand side. On the right-hand side, we will write the same array elements but this time in reverse order. We can also create a reusable function that can swap the specified index of the array. Syntax: [a[m], a[n]] = [a[n], a[m]] // Where m and n are the index numbers to swapExample 1: In this example, we will swap two number array elements in a single line using JavaScript. JavaScript let arr = [1, 2, 3, 5, 4]; // Swapping element at index 3 with // index 4 [arr[3], arr[4]] = [arr[4], arr[3]]; // Print the array console.log(arr); Output[ 1, 2, 3, 4, 5 ] Example 2: In this example, we will swap two string array elements in a single line using JavaScript. JavaScript let arr = ["e", "b", "c", "d", "a"]; // Swapping element at index 0 with // index 4 [arr[0], arr[4]] = [arr[4], arr[0]]; // Print the array console.log(arr); Output[ 'a', 'b', 'c', 'd', 'e' ] Using XOR Bitwise OperatorUsing the XOR bitwise operator, you can swap two elements without a temporary variable. This is done by sequentially applying XOR operations to the elements. This works because XORing a number twice with the same number returns the original number, effectively swapping the values. Example: In this example we swaps the elements at index1 and index2 in the array arr using the XOR bitwise operator. JavaScript let arr = [1, 2, 3, 4, 5]; let index1 = 1; let index2 = 3; arr[index1] ^= arr[index2]; arr[index2] ^= arr[index1]; arr[index1] ^= arr[index2]; console.log(arr); Output[ 1, 4, 3, 2, 5 ] Using Array.spliceUsing Array.splice, remove the elements to be swapped from the array, then insert them back into their new positions. This method effectively swaps the elements in place without needing additional temporary variables. Example : JavaScript let array = [1, 2, 3, 4]; array.splice(0, 2, ...array.slice(1, 3), array[0]); console.log(array); // [2, 1, 3, 4] Output[ 2, 3, 1, 3, 4 ] Comment More infoAdvertise with us Next Article Swapping two array elements in a single line using JavaScript S shivam70 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to rotate array elements by using JavaScript ? Given an array containing some array elements and the task is to perform the rotation of the array with the help of JavaScript. There are two approaches that are discussed below: Using Array unshift() and pop() MethodsUsing Array push() and shift() MethodsMethod 1: Using Array unshift() and pop() Me 2 min read How to swap key and value of JSON element using JavaScript ? In this article, we will swap the key and value of JSON elements using JavaScript. Given a JSON object and the task is to swap the JSON object key with values and vice-versa with the help of JavaScript. Below are the following approaches: Using for loopUsing Object.keys() and ForEach() MethodUsing O 2 min read How to create a string by joining the elements of an array in JavaScript ? Given an array containing array elements and here we will join all the array elements to make a single string. To join the array elements we use arr.join() method. There are two methods by which we can create a string by joining the elements of an array: Table of Content Using arr.join() MethodUsing 2 min read Delete the first element of array without using shift() method in JavaScript Given an array containing some array elements and the task is to remove the first element from the array and thus reduce the size by 1. We are going to perform shift() method operation without actually using it with the help of JavaScript. There are two approaches that are discussed below: Table of 2 min read How to Move a Key in an Array of Objects using JavaScript? The JavaScript array of objects is a type of array that contains JavaScript objects as its elements.You can move or add a key to these types of arrays using the below methods in JavaScript:Table of ContentUsing Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() met 5 min read How to Merge/Combine Arrays using JavaScript? Given two or more arrays, the task is to merge (or combine) arrays to make a single array in JavaScript. The simplest method to merge two or more arrays is by using array.concat() method.Using Array concat() MethodThe contact() method concatenates (joins) two or more arrays. It creates a new array a 2 min read How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met 2 min read How to Shuffle an Array using JavaScript ? To shuffle a JavaScript array we can use the Fisher-Yates shuffle also known as knuth shuffle. It will sort the given array in a random order with the help of the math.random() function.1. Shuffle JavaScript Array Using Fisher-Yates ShuffleThe Fisher-Yates Shuffle iterates through the array in rever 3 min read How to find every element that exists in any of two given arrays once using JavaScript ? In this article, we will learn how to find every element that exists in any of the given two arrays. To find every element that exists in any of two given arrays, you can merge the arrays and remove any duplicate elements. Table of Content Using SetUsing loopUsing filter() and concat()Using reduce a 3 min read How to Swap Array Object Values in JavaScript ? We have given the array of objects, and our task is to swap the values of the object keys present in the array of objects. Below is an example for a better understanding of the problem statement. Example:Input: arr = [{a: 1, b: 2}, {a:3, b: 4}]Output: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]Explnation: Th 4 min read Like