JavaScript-Boolesche Werte


Inhaltsverzeichnis

    Inhaltsverzeichnis anzeigen

Ein JavaScript-Boolescher Wert stellt einen von zwei Werten dar: true oder falsch.

Boolesche Werte

Sehr oft benötigen Sie beim Programmieren einen Datentyp, der nur einen haben kann aus zwei Werten, wie

  • JA NEIN

  • AN AUS

  • WAHR FALSCH

Dafür verfügt JavaScript über einen Boolean-Datentyp. Es kann nur Nehmen Sie die Werte true oder false an.


Die Boolean()-Funktion

Sie können die Funktion Boolean() verwenden, um herauszufinden, ob ein Ausdruck (oder eine Variable) vorhanden ist WAHR:

Beispiel

Boolean(10 > 9)

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Display the value of Boolean(10 &gt; 9):</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Boolean(10 > 9);
</script>

</body>
</html>

Oder noch einfacher:

Beispiel

(10 > 9)
10 > 9

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Display the value of 10 &gt; 9:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 10 > 9;
</script>

</body>
</html>

Vergleiche und Bedingungen

Das Kapitel JS-Vergleiche gibt einen vollständigen Überblick über Vergleichsoperatoren.

Das Kapitel JS-Bedingungen gibt einen vollständigen Überblick über bedingte Anweisungen.

Hier sind einige Beispiele:

==

Beschreibung: gleich

Beispiel:

if (day == "Monday")

>

Beschreibung: größer als

Beispiel:

if (salary > 9000)

<

Beschreibung: weniger als

Beispiel:

if (age < 18)

Der boolesche Wert eines Ausdrucks ist die Grundlage für alle JavaScript-Vergleiche und -Bedingungen.



Alles mit einem „Wert“ ist wahr

Beispiele

100

3.14

-15

"Hello"

"false"

7 + 1 + 3.14

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
"100 is " + Boolean(100) + "<br>" +
"3.14 is " + Boolean(3.14) + "<br>" +
"-15 is " + Boolean(-15) + "<br>" +
"Any (not empty) string is " + Boolean("Hello") + "<br>" +
"Even the string 'false' is " + Boolean('false') + "<br>" +
"Any expression (except zero) is " + Boolean(1 + 7 + 3.14);
</script>

</body>
</html>

Alles ohne „Wert“ ist falsch

Der boolesche Wert von 0 (Null) ist falsch:

let x = 0;
Boolean(x);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of 0:</p>

<p id="demo"></p>

<script>
let x = 0;
document.getElementById("demo").innerHTML = Boolean(x);
</script>

</body>
</html>

Der boolesche Wert von -0 (minus Null) ist falsch:

let x = -0;
Boolean(x);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of  -0:</p>

<p id="demo"></p>

<script>
let x = -0;
document.getElementById("demo").innerHTML = Boolean(x);
</script>

</body>
</html>

Der boolesche Wert von "" (leerer String) ist false:

let x = "";
Boolean(x);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of "":</p>

<p id="demo"></p>

<script>
let x = "";
document.getElementById("demo").innerHTML = Boolean("");
</script>

</body>
</html>

Der boolesche Wert von undefiniert ist false:

let x;
Boolean(x);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of undefined:</p>

<p id="demo"></p>

<script>
let x;
document.getElementById("demo").innerHTML = Boolean(x);
</script>

</body>
</html>

Der boolesche Wert von null ist false:

let x = null;
Boolean(x);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of null:</p>

<p id="demo"></p>

<script>
let x = null;
document.getElementById("demo").innerHTML = Boolean(x);
</script>

</body>
</html>

Der boolesche Wert von false ist (Sie haben es erraten) false:

let x = false;
Boolean(x);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of false:</p>

<p id="demo"></p>

<script>
let x = false;
document.getElementById("demo").innerHTML = Boolean(x);
</script>

</body>
</html>

Der boolesche Wert von NaN ist false:

let x = 10 / "Hallo";
Boolean(x);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Display the Boolean value of NaN:</p>

<p id="demo"></p>

<script>
let x = 10 / "Hello";
document.getElementById("demo").innerHTML = Boolean(x);
</script>

</body>
</html>

JavaScript-Boolesche Werte als Objekte

Normalerweise sind JavaScript-Boolesche Werte, die aus Literalen erstellt werden:

let x = false;

Boolesche Werte können aber auch mit dem Schlüsselwort new als Objekte definiert werden:

let y = new Boolean(false);

Beispiel

let x = false;
let y = new Boolean(false);

 //
typeof x returns boolean
 //
typeof y returns object

Erstellen Sie keine booleschen Objekte.

Das Schlüsselwort new verkompliziert den Code und verlangsamt die Ausführungsgeschwindigkeit.

Boolesche Objekte können zu unerwarteten Ergebnissen führen:

Bei Verwendung des Operators == sind x und y gleich:

let x = false;
let y = new Boolean(false);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Booleans and Boolean objects cannot be safely compared:</p>

<p id="demo"></p>

<script>
let x = false;         // x is a boolean
let y = new Boolean(false);  // y is an object
document.getElementById("demo").innerHTML = (x==y);
</script>

</body>
</html>

Bei Verwendung des Operators === sind x und y nicht gleich:

let x = false;
let y = new Boolean(false);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Booleans and Boolean objects cannot be safely compared:</p>

<p id="demo"></p>

<script>
let x = false;         // x is a Boolean
let y = new Boolean(false);  // y is an object
document.getElementById("demo").innerHTML = (x===y);
</script>

</body>
</html>

Beachten Sie den Unterschied zwischen (x==y) und (x===y).

(x == y) wahr oder falsch?

let x = new Boolean(false);
let y = new Boolean(false);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Booleans and Boolean objects cannot be safely compared.</p>

<p id="demo"></p>

<script>
const x = new Boolean(false);
const y = new Boolean(false);
document.getElementById("demo").innerHTML = (x==y);
</script>

</body>
</html>

(x === y) wahr oder falsch?

let x = new Boolean(false);
let y = new Boolean(false);

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Booleans</h1>
<p>Booleans and Boolean objects cannot be safely compared.</p>

<p id="demo"></p>

<script>
const x = new Boolean(false);
const y = new Boolean(false);
document.getElementById("demo").innerHTML = (x===y);
</script>

</body>
</html>

Der Vergleich zweier JavaScript-Objekte gibt immer false zurück.

Vollständige boolesche Referenz

Eine vollständige Referenz finden Sie in unserem Complete Boolesche JavaScript-Referenz.

Die Referenz enthält Beschreibungen und Beispiele aller booleschen Eigenschaften und Methoden.