javascript 变量值的交换
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maxi...
·
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
<script>
// 1. 交换两个变量
let n1 = 5;
let n2 = 6;
let tmp;
tmp = n1;
n1 = n2;
n2 = tmp;
console.log(n1 ,n2);
// 2. 交换两个变量值,不使用第三个变量
let m1 = 5;
let m2 = 6;
m1 = m1 + m2; // 5 + 6 = 11
m2 = m1 - m2; // 11 - 6 = 5
m1 = m1 - m2; // 11 - 5 = 6
console.log(m1, m2)
</script>
</html>
更多推荐

所有评论(0)