如何在JavaScript中比较字符串和布尔值?
在本教程中,我们将学习如何比较JavaScript中的字符串和布尔值。JavaScript具有一个相等运算符来比较一个值与另一个值。
JavaScript中有两种类型的相等运算符;一种是**相等运算符**(==),另一种是**严格相等运算符**(===)。两者之间的区别在于它们比较操作数的方式不同。
**相等运算符**只比较两个操作数的值,并返回布尔值。
**严格相等运算符**首先比较两个操作数的值,如果值匹配,则比较两个操作数的数据类型。这样,如果两个操作数的值和数据类型都相等,则严格相等运算符返回true。否则,它返回false值。
在本教程中,我们将使用*相等运算符*和*严格相等运算符*来比较字符串值和布尔值。
True和False的实际值
在继续本教程之前,我们应该了解布尔值**true**和**false**的值。true的实际值是**1**,false的值是0或**空字符串**""。
true == 1; false == 0; false == "";
现在,用户可以理解下面方法示例的输出了。
使用相等运算符(==)
我们将在这个方法中使用相等运算符来比较字符串和布尔值。使用相等运算符并不难,因为它是在JavaScript的第一个版本中引入的,大多数人都熟悉它。
语法
用户可以遵循以下语法使用相等运算符来比较字符串和布尔值。
false == "0"; //true true == "1"; //true false == ""; //true false == "false"; //false true == "true"; //false
示例
在下面的示例中,我们取了不同的字符串值来与true和false布尔值进行比较,并观察了结果。
<html>
<head>
<title> Comparing the string to Boolean.</title>
</head>
<body>
<h2>Comparing the string to Boolean.</h2>
<h4>Compare true with "1" string value:</h4>
<p id="result1"></p>
<h4>Compare false with "0" string value:</h4>
<p id="result2"></p>
<h4>Compare true with "hello" string value:</h4>
<p id="result3"></p>
<h4>Compare false with "hello" string value:</h4>
<p id="result4"></p>
<script>
let result1 = document.getElementById("result1");
let result2 = document.getElementById("result2");
let result3 = document.getElementById("result3");
let result4 = document.getElementById("result4");
let booleanValue = true == "1";
result1.innerHTML = booleanValue; // prints true
booleanValue = false == "0";
result2.innerHTML = booleanValue; // prints true
booleanValue = true == "hello";
result3.innerHTML = booleanValue; // prints false
booleanValue = false == "hello";
result4.innerHTML = booleanValue // prints false
</script>
</body>
</html>在上面的输出中,用户可以看到,当我们将true与“1”比较,将false与“0”比较时,它返回true布尔值。这是因为true的实际值是1,false的值是0。
使用严格相等运算符(===)
在这个方法中,我们将使用严格相等运算符来比较字符串和布尔值。当我们比较字符串和布尔值时,严格相等始终返回false,因为它也检查两个操作数的数据类型。
语法
用户可以遵循以下语法使用严格相等运算符和字符串与布尔值操作数。
true === "1" // returns false value false === "" // returns false value
尝试以下代码片段:
var data = true; data === "true" //false String(data) === "true" //true
示例
在这个例子中,我们将观察使用严格相等运算符将字符串与布尔值匹配时的输出。
<html>
<head>
<title>Comparing the string to Boolean.</title>
</head>
<body>
<h2>Comparing the string to Boolean.</h2>
<h4>Compare true with "1" string value using strict equality operator:</h4>
<p id="result1"></p>
<h4> Compare false with "0" string value using strict equality operator:</h4>
<p id="result2"></p>
<h4>Compare true with "123" string value using strict equality operator:</h4>
<p id="result3"></p>
<script>
let result1 = document.getElementById("result1");
let result2 = document.getElementById("result2");
let result3 = document.getElementById("result3");
let booleanValue = true === "1";
result1.innerHTML = booleanValue; // prints false
booleanValue = false === "0";
result2.innerHTML = booleanValue; // prints false
booleanValue = true === "123";
result3.innerHTML = booleanValue; // prints false
</script>
</body>
</html>在上面的输出中,用户可以看到,与之不同的是,它对所有字符串和布尔值的比较都返回false。
结论
当用户使用严格相等运算符来比较字符串和布尔值时,它总是返回false。所以,它不意味着在任何地方使用。而且,很少有情况需要我们比较字符串和布尔值,用户在这种情况下可以使用相等运算符。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP