3 ms·
This is JS, so you should use de-structuring to swap in one easy-to-follow step [1]: let a = 5; let b = 10; [a,b] = [b,a]; console.log(a,b) //
by sam_goody 5y ago
This is JS, so you should use de-structuring to swap in one easy-to-follow step [1]:
let a = 5;
let b = 10;
[a,b] = [b,a];
console.log(a,b) // 10, 5
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- mkdirp 5y agoDoesn't this create a new (temporary) array of value `[b,a]`?
- ncmncm 5y agoIf it does, that was optimal. If it doesn't, that was optimal. Outsmarting your optimizer is harder than that nowadays.
- rurban 5y agoif optimizers would be smart. they are not. they do bad things, and occasionally even wrong things. and usually compiler writers blame the user, not themselves then.
- deleted 5y ago[deleted]