How to Swap Two Values Without Temporary Variable in Javascript

In this article, you will learn how to swap two values without a temporary variable in Javascript. Let’s say you have 2 variables. In order…

The post How to Swap Two Values Without Temporary Variable in Javascript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli

In this article, you will learn how to swap two values without a temporary variable in Javascript.

Let’s say you have 2 variables.

// A number variable called 'a' with value 123
var a = 123;

// A number variable called 'b' with value 456
var b = 456;

In order to swap two values without a temporary variable, you can use the ES6’s destructuring assignment. In this example, you will be swapping the values of variable ‘a’ with value of variable ‘b’.

// A number variable called 'a' with value 123
var a = 123;

// A number variable called 'b' with value 456
var b = 456;

// Value of variable 'b' is assigned to variable 'a' and vice versa
[a, b] = [b, a];

console.log(a);
// => 456

console.log(b);
// => 123

Note: The ES6’s destructuring assignment functions by extracting values from its right operand and assigning them to its left operand.

The post How to Swap Two Values Without Temporary Variable in Javascript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli


Print Share Comment Cite Upload Translate Updates
APA

Ariessa Norramli | Sciencx (2021-02-21T10:06:22+00:00) How to Swap Two Values Without Temporary Variable in Javascript. Retrieved from https://www.scien.cx/2021/02/21/how-to-swap-two-values-without-temporary-variable-in-javascript/

MLA
" » How to Swap Two Values Without Temporary Variable in Javascript." Ariessa Norramli | Sciencx - Sunday February 21, 2021, https://www.scien.cx/2021/02/21/how-to-swap-two-values-without-temporary-variable-in-javascript/
HARVARD
Ariessa Norramli | Sciencx Sunday February 21, 2021 » How to Swap Two Values Without Temporary Variable in Javascript., viewed ,<https://www.scien.cx/2021/02/21/how-to-swap-two-values-without-temporary-variable-in-javascript/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Swap Two Values Without Temporary Variable in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/21/how-to-swap-two-values-without-temporary-variable-in-javascript/
CHICAGO
" » How to Swap Two Values Without Temporary Variable in Javascript." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/02/21/how-to-swap-two-values-without-temporary-variable-in-javascript/
IEEE
" » How to Swap Two Values Without Temporary Variable in Javascript." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/21/how-to-swap-two-values-without-temporary-variable-in-javascript/. [Accessed: ]
rf:citation
» How to Swap Two Values Without Temporary Variable in Javascript | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/02/21/how-to-swap-two-values-without-temporary-variable-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.