交换两个变量的值,使用临时变量用来做中间存储。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>js变量练习</title>
<script>
//1、创建一个临时变量 temp
//2、把 a1 的值传给 temp
//3、把 a2 的值传给 a1
//4、把 temp 的值传给 a2
var temp;
var a1 = '原始a1';
var a2 = '原始a2';
temp = a1;
a1 = a2;
a2 = temp;
console.log(
'新的a1:'+a1,
'新的a2:'+a2
);
</script>
</head>
<body>
</body>
</html>
评论前必须登录!
注册