Skip to content

swapping vars without temp var #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
layout: *post

title: Swapping 2 values without temp variable
tip-number: xx
tip-username: gk3000
tip-username-profile: https://twitter.com/gk3000
tip-tldr: Swapping two values in a single expression thanks to destructuring
<!-- tip-writer-support: Paypal, Coinbase, Etc -->
<!-- tip-translator-support: Paypal, Coinbase, Etc -->

categories:
- en
---
With array destructuring it's really easy to swap two values in a single expression without need of a temporary variable.

```js
let foo = 10, bar = 5;
[foo, bar] = [bar, foo]
```

That's it, now `foo` is 5 and `bar` is 10.

JS first evaluates newly created array on the right of the assignment operator and then destructures it according to the order of vars on the left and since they are the same but in reverse order their values are swapped.

Works for array's items as well:

```js
let foo = [1,2,3,4,5];
[foo[0], foo[4]] = [foo[4],foo[0]]
```

And objects:

```js
let foo = {a:1,b:2,c:3};
[foo.a,foo.b]=[foo.b,foo.a]
```

👻