How to swap two variables in JavaScript

javascript program to swap two numbers without using third variable
javascript swap array
swapping in javascript with example
javascript swap array elements es6
javascript swap object values
es6 swap variables
swap string in javascript
how swap two values without temporary variables using javascript

I have this two variables:

var a = 1,
    b = 2;

My question is how to swap them? Only this variables, not any objects.

Here's a one-liner to swap the values of two variables. Given variables a and b:

b = [a, a = b][0];

Demonstration below:

var a=1,
    b=2,
    output=document.getElementById('output');

output.innerHTML="<p>Original: "+a+", "+b+"</p>";

b = [a, a = b][0];

output.innerHTML+="<p>Swapped: "+a+", "+b+"</p>";
<div id="output"></div>

How to swap two variables in JavaScript, Swapping the value of two variables normally takes three lines and a temporary variable. What if I told you there was an easier way to do this with JavaScript? It works in two steps: 1. (b=a, 0) sets b to the old value of a and yields 0 2. a = b + 0 sets a to the old value of b. Another single line swapping: b=a+(a=b)-b. Single line swapping with XOR

ES6 (Firefox and Chrome already support it (Destructuring Assignment Array Matching)):

let a = 5, b = 6;
[a, b] = [b, a];
console.log(`${a} ${b}`);

How To Swap Two Values Without Temporary Variables Using , Swap Variables in JavaScript – Different Methods. Swap variable values using a temporary variable. // Using a Temp Variable var x = 10; var y = 20; var tmp = x; x = y; y = tmp; alert("Value of X=" + x + " and value of Y=" + y); Swap variable values without a temporary variable. Swap using Bitwise XOR operator. Single Here are some different ways to swap variable values in JavaScript. I will show swapping using a temporary variable, without a temporary variable, XOR operator and a single line swap. Swap variable values using a temporary variable

You can do this:

var a = 1,
    b = 2,
    tmp;
tmp = a;
a = b;
b = tmp;

For readability and maintainability, this can't be beat (at least in JavaScript). Anybody maintaining the code (including you six months from now) will know exactly what's going on.

Since these are integers, you can also use any number of clever tricks1 to swap without using a third variable. For instance you can use the bitwise xor operator:

let a = 1, b = 2;
a = a ^ b;
b = a ^ b;
a = a ^ b;
    
console.log('a is now:', a);
console.log('b is now:', b);

Swap Variables in JavaScript – Different Methods, Let's break down 5 different ways of swapping two variables in JavaScript, Here is the first one. #1 Using a Temp Variable. Swapping two variables refers to mutually exchanging the values of the variables. Generally, this is done with the data in memory. The simplest method to swap two variables is to use a third temporary variable :

All you need to know about swapping two variables in JavaScript , Swapping the value of two variables normally takes three lines and a temporary variable. What if I told you there was an easier way to do this  Don't use the code below. It is not the recommended way to swap the values of two variables (simply use a temporary variable for that). It just shows a JavaScript trick. This solution uses no temporary variables, no arrays, only one addition, and it's fast. In fact, it is sometimes faster than a temporary variable on several platforms.

Since ES6, you can also swap variables more elegantly:

var a = 1,
    b = 2;

[a, b] = [b, a];

console.log('a:', a, 'b:', b); // a: 2 b: 1

Swapping variables with JavaScript · A Beautiful Site, javascript. The usual way to swap the values of two variables is usually somethign like this: var temp = var1; var1 =var2; var2 = temp;. How do I swap two variables with a function in Javascript? Having posted the above solution, I will say I agree with Rhomboid: "there's probably a much better way to solve whatever problem you're trying to solve."

Swap values of two variables without using a temporary variable, In this video tutorial, you will learn how to swap two variables without using third variable in Duration: 2:57 Posted: Jan 22, 2020 Swap two Number Program in JavaScript - Here we write program to swap two number using third variable in javascript. HOME C C++ DS Java AWT Collection Jdbc JSP Servlet SQL PL/SQL C-Code C++-Code Java-Code Project Word Excel

How to Swap Two Variables Without Using Third Variable in Javascript, This is the JavaScript forum - a completely different programming language from java. Also there is no way to swap two numbers without using  In programming, just like in algebra, we use variables (like price1) to hold values. In programming, just like in algebra, we use variables in expressions (total = price1 + price2). From the example above, you can calculate the total to be 11. JavaScript variables are containers for storing data values.

How to Swap two numbers without using third variable in Java , Given two variables, x and y, swap two variables without using a third variable. Method 1 (Using Arithmetic Operators) The idea is to get sum in one of the two  The bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ. For example XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7 (0111) and 5 (0101) is (0010).

Comments
  • Swap (computer science)#Using a temporary variable
  • +1. But the shortest version will be in ECMAScript 6: [a, b] = [b, a];.
  • @Kay: It also seems to be much slower using an array instead of a third variable: http://jsperf.com/swap-array-vs-variable I only tested this in Chrome though. I wasn't able to test ECMAScript 6 version yet as it currently gives a Invalid left-hand side in assignment error.
  • @FrançoisWahl Good point. I think most of the answers here will work and are fairly equivalent. I suppose it's a trade off between temporary variable use, amount of code, and speed.
  • @FrançoisWahl well, I would not have guessed that this solution was so much slower. See also: jsperf.com/swap-array-vs-variable/3
  • @showdev Read the Dijkstra quote in Ted Hopp's answer.
  • anyone knows the name of such kind of swap in es6?
  • @derek - I think it's called array matching, a form of destructuring assignment.
  • This appears to be about 35 times slower than the third variable method on nodejs 7.4.0/win7 64. But it sure is neat.
  • Thanks for the name. developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
  • Since V8 version 6.8, swapping variables with array destructuring should be as fast as with a temporary variable (v8project.blogspot.com/2018/06/v8-release-68.html).
  • Xor will work with any data type. It's the subtraction trick that will only work with numbers.