How to swap two variables in JavaScript

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>

Swap rows with columns (transposition) of a matrix in javascript [duplicate]

DuckDucking turned up this by Ken. Surprisingly, it’s even more concise and complete than Nikita’s answer. It retrieves column and row lengths implicitly within the guts of map(). function transpose(a) { return Object.keys(a[0]).map(function(c) { return a.map(function(r) { return r[c]; }); }); } console.log(transpose([ [1,2,3], [4,5,6], [7,8,9] ]));

Java method to swap primitives

I think this is the closest you can get to a simple swap, but it does not have a straightforward usage pattern: int swap(int a, int b) { // usage: y = swap(x, x=y); return a; } y = swap(x, x=y); It relies on the fact that x will pass into swap before y is … Read more

What can I do with a moved-from object?

17.6.5.15 [lib.types.movedfrom] Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state. When an object is in an unspecified state, you can perform any operation on the … Read more