Replacement of two numbers

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u010263423/article/details/102747084

Replacement of two numbers

The first: third parameter

	var a = 5;
	var b = 6;
	var t = a;
	    a = b;
	    b = t;

The second: addition and subtraction

	a = a + b;
    b = a - b;
    a = a - b;

Third: Array

	a = [a,b];
    b = a[0];
    a = a[1];

Fourth: XOR (efficiency)

Symbol is XOR ^, two numbers into 2进制the same bit is 0, except 1 bit is
alsoa ^ b ^ a = b;

    a = a ^ b;
    b = a ^ b;
    a = a ^ b;

Fifth: show operation

	a = [b,b = a][0];
	//其中[b,b = a]是数组,[0]是下标
	//这个最重要的是优先级,b 是第一个,然后 b = a,最后是a的赋值

Sixth: Object

	var a = 5;
    var b = 6;
    a = {a:a,b:b};
    b = a.a;
    a = a.b;
    console.log(a,b);

Guess you like

Origin blog.csdn.net/u010263423/article/details/102747084