JavaScript - Swap Two Variables in JavaScript Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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. JavaScript let a = 40; let b = 30; console.log(`before swap a= ${a}`); console.log(`before swap b= ${b}`); // a would be swapped to b and b would be swapped to a [b, a] = [a, b]; console.log(`after swap a= ${a}`); console.log(`after swap b= ${b}`); Outputbefore swap a= 40 before swap a= 30 after swap a= 30 after swap a= 40 Using Temporary variableLet's say we create a temp variable that stores the value of a variable A.Then we copy the value of B to A (A would be overwritten).Then we copy the value of temp to B (earlier value of A). JavaScript let a = 20; let b = 10; let temp; console.log(`before swapping: a= ${a}`); console.log(`before swapping b= ${b}`); temp = a; a = b; b = temp; console.log(`after swapping a= ${a}`); console.log(`after swapping b= ${b}`); Outputbefore swapping: a= 20 before swapping b= 10 after swapping a= 10 after swapping b= 20 Using Arithmetic OperationsFirstly, we add a + b to a (a would be greater than b as it is).Now we subtract b from a so the value of b is now available to bNow we subtract a with b again to a so a will have the value of B JavaScript let a = 10; let b = 20; console.log(`before swap a= ${a}`); console.log(`before swap b= ${b}`); a = a + b;//10=10+20 now a would be 30 b = a - b;//20=30-20 now b would be 10 a = a - b;//30=30-10 so a would be now 20 console.log(`after swap a= ${a}`); console.log(`after swap b= ${b}`); Outputbefore swap a= 10 before swap b= 20 after swap a= 20 after swap b= 10 Using XOR Bitwise Operatorthe XOR bitwise operation is used to swap the values without the need for a temporary variable. This method takes advantage of the property that a ^ b ^ b is equal to a. JavaScript let a = 5; let b = 10; console.log("Before swapping: a =", a, "b =", b); a = a ^ b; b = a ^ b; a = a ^ b; console.log("After swapping: a =", a, "b =", b); OutputBefore swapping: a = 5 b = 10 After swapping: a = 10 b = 5 Comment More info J jeetpurohit989 Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2022 JavaScript-Questions +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like