Home >Web Front-end >JS Tutorial >不使用中间变量,交换int型的 a, b两个变量的值。_javascript技巧

不使用中间变量,交换int型的 a, b两个变量的值。_javascript技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:17:301210browse
复制代码 代码如下:

//by ppchen
var a = 10, b = 2;
a = a + b;
b = a - b;
a = a - b;

复制代码 代码如下:

//by Mr.司马
var a = 10000,b=245
a = a^b;
b = b^a;
a = a ^b;

复制代码 代码如下:

//by cloudgamer
var a = 33,b=44
a=[a,b]
b=a[0]
a=a[1]

复制代码 代码如下:

//by Mr.司马
var a = 23,b=454;
a = {a:b,b:a};
b = a.b;
a = a.a;

评:1、2种解法是普遍解,只能交换数字,而3、4种则适用性更广。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn