
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to swap variables with destructuring in JavaScript?
Swapping variables has become very easy with destructuring. In contemporary javascript swapping takes place by using another variable. It may not be hectic but it is lengthy. But in modern javascript there is no need for the third variable. Let's discuss it in detail.
Example-1
In the following example, swapping has done using another variable called "temp". Therefore the code got lengthier.
<html> <body> <script> var a = "Sachin"; var b = "Tendulkar"; document.write("Before swapping-"+ " "+ a + " " +b); var tmp = a; a = b; b = tmp; document.write("</br>"); document.write("After swapping-"+ " " + a + " " +b); </script> </body> </html>
Output
Before swapping- Sachin Tendulkar After swapping- Tendulkar Sachin
The task of swapping has become easier because of destructuring. Here we don't need to use another variable and even the code is not lengthy.
Example-2
In the following example, no third variable is used and the swapping has done with destructuring. Here the code is much smaller than the above code.
<html> <body> <script> var a = "Sachin"; var b = "Tendulkar"; document.write("Before swapping-"+ " "+ a + " " +b); [a,b] = [b,a]; document.write("</br>"); document.write("After swapping-"+ " " + a + " " +b); </script> </body> </html>
Output
Before swapping- Sachin Tendulkar After swapping- Tendulkar Sachin
- Related Articles
- How to swap variables using Destructuring Assignment in JavaScript?
- How to swap two variables in JavaScript?
- How to Swap Two Variables using Python?
- How to swap two String variables without third variable.
- Destructuring and function parameters in JavaScript
- How to set default values when destructuring an object in JavaScript?
- What is JavaScript object Destructuring?
- How to pass JavaScript Variables with AJAX calls?
- Swap two variables in one line in Java
- How to swap two array elements in JavaScript?
- Swap two variables in one line using C#
- How to declare variables in JavaScript?
- How to name variables in JavaScript?
- Swap two variables in one line in C/C+
- Swap two variables in one line in using Java

Advertisements