Everything Without a "Value" is False
The Boolean value of 0 (zero) is false
<p id="demo"></p>
<script>
let x = 0;
document.getElementById("demo").innerHTML = Boolean(x);
</script>
false
The Boolean value of -0 (minus zero) is false.
<p id="demo"></p>
<script>
let x = -0;
document.getElementById("demo").innerHTML = Boolean(x);
</script>
false
The Boolean value of "" (empty string) is false
<p id="demo"></p>
<script>
let x = "";
document.getElementById("demo").innerHTML = Boolean("");
</script>
false
The Boolean value of undefined is false
<p id="demo"></p>
<script>
let x;
document.getElementById("demo").innerHTML = Boolean(x);
</script>
false
The Boolean value of null is false
The Boolean value of NaN is false
<p id="demo"></p>
<script>
let x = 10 / "Hello";
document.getElementById("demo").innerHTML = Boolean(x);
</script>
false
Booleans as Objects
let x = false;
let y = new Boolean(false);
// typeof x returns boolean
// typeof y returns object
When using the == operator, x and y are equa.
When using the === operator, x and y are not equal.
let x = false;
let y = new Boolean(false);
Do not create Boolean objects.
objects always return false.
'JavaScript' 카테고리의 다른 글
JavaScript - For In, For of (0) | 2022.11.17 |
---|---|
JavaScript - Comparison (0) | 2022.11.17 |
JavaScript - Math Object (0) | 2022.11.17 |
JavaScript - Set Date Methods (0) | 2022.11.17 |
JavaScript - Date Formats (0) | 2022.11.16 |