JavaScript 將字串轉換為 boolean
在 JavaScript 中將字串轉換為 boolean,如下所示:-
示例
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
</style>
</head>
<body>
<h1>Converting string to boolean</h1>
<button class="Btn">CLICK HERE</button>
<input class="str" type="text" />
<p class="sample"></p>
<h3>Click the above button to convert the string to boolean value</h3>
<script>
let sampleEle = document.querySelector(".sample");
let inputEle = document.querySelector(".str");
document.querySelector(".Btn").addEventListener("click", () => {
sampleEle.innerHTML += "Using == " + (inputEle.value == "true") + "<br>";
sampleEle.innerHTML += "Using === " + (inputEle.value === "true") + "<br>";
});
</script>
</body>
</html>輸出

在輸入欄位中輸入非字串“true”,然後單擊按鈕:-

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP