How to Swap Variables using Destructuring Assignment in JavaScript? Last Updated : 12 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Destructuring assignment in JavaScript allows us to unpack the values from arrays or objects and assign them to multiple variables.Syntax// Destructuring from Array, arr = [ 10, 15 ]let [ a, b ] = arr; // a=> 10, b => 15// Destructuring from Array, obj = { n1: 10, n2: 15 ]let [ n1, n2 ] = obj;JavaScript destructing assignment is used to swap the elements by swapping the variable positions in the destructuring array.Syntax// Swaping the values of a and b[ a, b] = [ b, a ]Example: This example uses the JavaScript destructuring assignment to swap the values of two variables. JavaScript // Given variables let a = 10; let b = 25; // Swap values using Destructuring Assignment [a, b] = [b, a]; // Display updated values console.log("Updated Values= a:",a, "b:",b ); OutputUpdated Values= a: 25 b: 10 Example 2: This example swaps the values of 3 variables using destructuring assignment in JavaScript. JavaScript // Given values // Assign values from array using Destructuring Assignment let [a, b, c] = [ 30, 40, 50 ]; // Swap values using Destructuring Assignment [a, b, c] = [b, c, a]; // Display updated values console.log("Updated Values= a:",a, "b:", b,"c:", c ); OutputUpdated Values= a: 40 b: 50 c: 30 Comment More infoAdvertise with us Next Article How to Swap Variables using Destructuring Assignment in JavaScript? vpsop Follow Improve Article Tags : JavaScript Web Technologies ES6 JavaScript-Questions Similar Reads How to set default values when destructuring an object in JavaScript ? Destructuring in JavaScript: Destructuring was introduced in ECMA Script 6 version. It is a concept that is used to unpack or free values from arrays and objects into variables. The values corresponding to array elements or object properties get stored in the variables. Example: This example shows t 2 min read How to unpack array elements into separate variables using JavaScript ? Given an array and the task is to unpack the value of the array into separate variables by using the preferred method using javascript.We can unpack the array elements into separate variables by the following methods:Table of ContentDestructuring AssignmentUsing Array.slice() MethodUsing the spread 2 min read JavaScript - Use Arrays to Swap Variables Here are the different methods to swap variables using arrays in JavaScript1. Using Array DestructuringArray destructuring is the most efficient and modern way to swap variables in JavaScript. This method eliminates the need for a temporary variable.JavaScriptlet x = 5; let y = 10; [x, y] = [y, x]; 3 min read JavaScript - Swap Two Variables in JavaScript In this article, we will learn about swapping 2 variables with Various Approaches.Using destructing Assignment - Most UsedHere, we will be using a destructing Assignment. The destructing assignment makes it possible to unpack values from an array, object, or other properties into distinct variables. 3 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 Declare Multiple Variables in JavaScript? JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.Decla 2 min read Difference Between Destructuring Assignment and Dot Notation in JavaScript JavaScript offers various ways to access and assign values from the objects and arrays. Two common methods are destructuring assignment and dot notation. Understanding the differences between these methods can help us write more efficient and readable code.These are the following topics that we are 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 Nullish Coalescing Assignment (??=) Operator in JavaScript This is a new operator introduced by javascript. This operator is represented by x ??= y and it is called Logical nullish assignment operator. Only if the value of x is nullish then the value of y will be assigned to x that means if the value of x is null or undefined then the value of y will be ass 2 min read How to Swap Two Array of Objects Values using JavaScript ? Swapping values between two arrays of objects is a common operation in JavaScript, there are multiple methods to swap values between arrays of objects efficiently. Swapping values between arrays can be useful in scenarios like reordering data, shuffling items, or performing certain algorithms. These 6 min read Like