Javascript教程
JavaScript基础
JavaScript Objects
JavaScript BOM
JavaScript DOM
JavaScript OOP
JavaScript Cookies
JavaScript事件
JavaScript异常
JavaScript常用

JavaScript 交换两个变量的程序

用于交换两个变量的 JavaScript 程序

在本例中,您将学习编写一个程序,使用各种方法在 JavaScript 中交换两个变量。
要理解此示例,您应该了解以下JavaScript 编程主题:
JavaScript 变量和常量 JavaScript 运算符

示例 1: 使用临时变量

//JavaScript program to swap two variables
//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
//create a temporary variable
let temp;
//swap variables
temp = a;
a = b;
b = temp;
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);
输出
Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4
这里,
我们创建了一个 temp 变量来临时存储 a 的值。 我们将 b 的值赋给 a temp 的值被赋值给 b
结果,变量的值被交换。
注意: 您还可以使用此方法交换字符串或其他数据类型。

示例 2: 使用 es6(ES2015) 解构赋值

//JavaScript program to swap two variables
//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
//using destructuring assignment
[a, b] = [b, a];
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);
输出
Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4
这里使用了一个新的 es6 特性,称为解构赋值 [a, b] = [b, a],用于交换两个变量的值。如果 [a, b] = [1, 2, 3],则 a的值为1, b的值为b 将是 2。
首先创建一个临时数组 [b, a]。这里 [b, a] 的值将是 [2, 4] 数组的解构完成,即[a, b] = [2, 4]
结果,变量的值被交换。
您可以在 JavaScript 析构赋值中了解有关解构的更多信息。
注意: 您还可以使用此方法交换字符串或其他数据类型。
您还可以使用算术运算符交换变量的值。

示例 3: 使用算术运算符

//JavaScript program to swap two variables
//take input from the users
let a = parseInt(prompt('Enter the first variable: '));
let b = parseInt(prompt('Enter the second variable: '));
// addition and subtraction operator
a = a + b;
b = a-b;
a = a-b;
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);
输出
Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4
此方法只使用两个变量,并使用算术运算符 +-交换变量的值。
这里使用 parseInt() 是因为 prompt() 将用户的输入作为字符串。当添加数字字符串时,它的行为就像一个字符串。例如, '2' + '3' = '23'。所以 parseInt() 将数字字符串转换为数字。
要了解有关类型转换的更多信息,请转到 JavaScript 类型转换。
让我们看看上面的程序如何交换值。最初, a 是 4, b 是 2。
a = a + b 将值 4 + 2 分配给 a(现在为 6)。 b = a-b 将值 6-2 分配给 b(现在为 4)。 a = a-b 将值 6-4 分配给 a(现在为 2)。
最后, a 是 2, b 是 4。
注意: 如果两个变量都是数字类型,则可以使用算术运算符(+,-)。

示例 4: 使用按位异或运算符

//JavaScript program to swap two variables
//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
// XOR operator
a = a ^ b
b = a ^ b
a = a ^ b
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);
输出
Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4
如果两个操作数不同,则按位异或运算符的计算结果为 true。要了解有关按位运算符的更多信息,请访问 JavaScript 按位运算符。
让我们看看上面的程序如何交换值。最初, a 是 4, b 是 2。
a = a ^ b 将值 4 ^ 2 分配给 a(现在为 6)。 b = a ^ b 将值 6 ^ 2 分配给 b(现在为 4)。 a = a ^ b 将值 6 ^ 4 分配给 a(现在为 2)。
最后, a 是 2, b 是 4。
注意: 您只能对整数(整数)值使用此方法。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4