Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Javascript swap old and new method

//old method 
var a = 1;
var b = 2;
var temp = a;
a = b;
b = temp;
console.log(a, b)
//expected output: 2 1

//new method(using destructuring method for swap)
var x = 10;
var y = 20;
[y, x] = [x, y];
console.log(y, x);
//expected output: y = 10 and x = 20
Source by dmitripavlutin.com #
 
PREVIOUS NEXT
Tagged: #Javascript #swap #method
ADD COMMENT
Topic
Name
2+8 =