diff --git a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md index 8869d32e6..f04d45948 100644 --- a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md +++ b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md @@ -1,4 +1,4 @@ -The answer is `2`, that's the first truthy value. +Die Antwort lautet `2`, das ist der erste effektiv wahre Wert. ```js run alert( null || 2 || undefined ); diff --git a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md index a7c9addfc..1e2a9e6d3 100644 --- a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md +++ b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# What's the result of OR? +# Welches Ergebnis liefert ODER ? -What is the code below going to output? +Was gibt der untenstehende Code aus ? ```js alert( null || 2 || undefined ); diff --git a/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md b/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md index f85b56366..5899d4cc1 100644 --- a/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md +++ b/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md @@ -1,13 +1,13 @@ -The answer: first `1`, then `2`. +Antwort: zuerst `1`, dann `2`. ```js run alert( alert(1) || 2 || alert(3) ); ``` -The call to `alert` does not return a value. Or, in other words, it returns `undefined`. +Der Aufruf von `alert` liefert keinen Wert zurück. Genauer formuliert liefert er `undefined`. -1. The first OR `||` evaluates its left operand `alert(1)`. That shows the first message with `1`. -2. The `alert` returns `undefined`, so OR goes on to the second operand searching for a truthy value. -3. The second operand `2` is truthy, so the execution is halted, `2` is returned and then shown by the outer alert. +1. Das erste ODER `||` wertet seinen linken Operanden `alert(1)` aus. Das führt zur Anzeige der ersten Nachricht `1`. +2. Der Aufruf von `alert` liefert `undefined`, also setzt ODER die Suche nach einen effektiv wahren Wert mit dem zweiten Operanden fort. +3. Der zweite Operand `2` ist effektiv wahr, die Ausführung wird gestoppt, `2` wird zurückgegeben und mittels des äußeren `alert` angezeigt. -There will be no `3`, because the evaluation does not reach `alert(3)`. +`3` wird nicht angezeigt, da die Auswertung `alert(3)` nicht erreicht. diff --git a/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md b/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md index 3908fa2ec..71c92d642 100644 --- a/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md +++ b/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md @@ -2,9 +2,9 @@ importance: 3 --- -# What's the result of OR'ed alerts? +# Was ergeben mit ODER verknüpfte `alert`-Aufrufe ? -What will the code below output? +Was gibt der untenstehende Code aus? ```js alert( alert(1) || 2 || alert(3) ); diff --git a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md index 5c2455ef4..7191b9ad5 100644 --- a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md +++ b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md @@ -1,4 +1,4 @@ -The answer: `null`, because it's the first falsy value from the list. +Antwort: `null`, denn das ist der erste effektiv nicht wahre Wert aus der Liste. ```js run alert( 1 && null && 2 ); diff --git a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md index 043d431e4..9495f51c9 100644 --- a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md +++ b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# What is the result of AND? +# Was ist das Ergebnis von UND? -What is this code going to show? +Was zeigt der untenstehende Code an? ```js alert( 1 && null && 2 ); diff --git a/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md b/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md index b6fb10d72..64efc4d6a 100644 --- a/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md +++ b/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md @@ -1,10 +1,10 @@ -The answer: `1`, and then `undefined`. +Antwort: `1`, anschließend `undefined`. ```js run alert( alert(1) && alert(2) ); ``` -The call to `alert` returns `undefined` (it just shows a message, so there's no meaningful return). +Der Aufruf von `alert` gibt `undefined` zurück (Die Funktion gibt nur eine Nachricht aus, ohne Rückgabewert). -Because of that, `&&` evaluates the left operand (outputs `1`), and immediately stops, because `undefined` is a falsy value. And `&&` looks for a falsy value and returns it, so it's done. +Deswegen wertet `&&` den linken Operanden aus (Ausgabe von `1`) und terminiert unmittelbar, da `undefined` ein effektiv nicht wahrer Wert ist. `&&` wiederum sucht nach einem effektiv nicht wahren Wert und gibt ihn zurück, das beendet somit die Auswertung. diff --git a/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md b/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md index 69f877b95..6c4f5fc4a 100644 --- a/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md +++ b/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md @@ -2,9 +2,9 @@ importance: 3 --- -# What is the result of AND'ed alerts? +# Was ist das Ergebnis mit UND verknüpfter `alert`-Aufrufe? -What will this code show? +Was gibt untenstehender Code aus? ```js alert( alert(1) && alert(2) ); diff --git a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md index 25e3568f8..d2ad14daa 100644 --- a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md +++ b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md @@ -1,16 +1,16 @@ -The answer: `3`. +Antwort: `3`. ```js run alert( null || 2 && 3 || 4 ); ``` -The precedence of AND `&&` is higher than `||`, so it executes first. +Die Präzedenz von UND `&&` ist höher als die von `||`, deswegen wird UND zuerst ausgeführt. -The result of `2 && 3 = 3`, so the expression becomes: +Das Ergebnis von `2 && 3` lautet `3`, damit wird der Ausdruck zu: ``` null || 3 || 4 ``` -Now the result is the first truthy value: `3`. +Das Ergebnis ist dann der erste effektiv wahre Wert: `3`. diff --git a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md index b18bb9c51..14ebe317f 100644 --- a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md +++ b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# The result of OR AND OR +# Das Ergebnis von ODER UND ODER -What will the result be? +Was ist das Ergebnis? ```js alert( null || 2 && 3 || 4 ); diff --git a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md index cc00ca9fc..d692bca9f 100644 --- a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md +++ b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md @@ -2,8 +2,6 @@ importance: 3 --- -# Check the range between +# Bereichsprüfung (positiv) -Write an "if" condition to check that `age` is between `14` and `90` inclusively. - -"Inclusively" means that `age` can reach the edges `14` or `90`. +Schreibe die Bedingung für eine Fallunterscheidung mit `if` um zu prüfen, daß `age` zwischen `14` and `90` liegt (Grenzen inklusive, `age` kann also die Werte `14` und `90` annehmen). diff --git a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md index d1946a967..009a7654f 100644 --- a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md +++ b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md @@ -1,10 +1,10 @@ -The first variant: +Erste Variante: ```js if (!(age >= 14 && age <= 90)) ``` -The second variant: +Zweite Variante: ```js if (age < 14 || age > 90) diff --git a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md index 7c22d6ad1..64f3e82ca 100644 --- a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md +++ b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md @@ -2,8 +2,8 @@ importance: 3 --- -# Check the range outside +# Bereichsprüfung (negativ) -Write an `if` condition to check that `age` is NOT between 14 and 90 inclusively. +Schreibe die Bedingung für eine Fallunterscheidung mit `if` um zu prüfen, dass `age` NICHT zwischen `14` und `90` liegt (Grenzen inklusive). -Create two variants: the first one using NOT `!`, the second one -- without it. +Formuliere 2 Varianten, die Erste mit NICHT `!`, die Zweite -- ohne. diff --git a/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md b/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md index 210509758..e5f9ba763 100644 --- a/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md +++ b/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md @@ -1,20 +1,19 @@ -The answer: the first and the third will execute. +Antwort: Erster und dritter Aufruf werden ausgeführt. Details: ```js run -// Runs. -// The result of -1 || 0 = -1, truthy -if (-1 || 0) alert( 'first' ); +// Ausgeführt. +// Das Resultat von -1 || 0 = -1, effektiv wahr +if (-1 || 0) alert( 'Erster' ); -// Doesn't run -// -1 && 0 = 0, falsy -if (-1 && 0) alert( 'second' ); +// Nicht ausgeführt +// -1 && 0 = 0, effektiv nicht wahr +if (-1 && 0) alert( 'Zweiter' ); -// Executes -// Operator && has a higher precedence than || -// so -1 && 1 executes first, giving us the chain: +// Ausgeführt +// Operator && hat höhere Präzedenz als ||, +// damit wird -1 && 1 zuerst ausgewertet, mit der Auswertereihung: // null || -1 && 1 -> null || 1 -> 1 -if (null || -1 && 1) alert( 'third' ); +if (null || -1 && 1) alert( 'Dritter' ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/8-if-question/task.md b/1-js/02-first-steps/11-logical-operators/8-if-question/task.md index 55987121b..2ee3eacf1 100644 --- a/1-js/02-first-steps/11-logical-operators/8-if-question/task.md +++ b/1-js/02-first-steps/11-logical-operators/8-if-question/task.md @@ -2,15 +2,15 @@ importance: 5 --- -# A question about "if" +# Eine Frage zu "if" -Which of these `alert`s are going to execute? +Welche der fogenden `alert`-Aufrufe werden ausgeführt? -What will the results of the expressions be inside `if(...)`? +Was sind die Ergebnisse der Ausdrücke in den Bedingungen `if(...)`? ```js -if (-1 || 0) alert( 'first' ); -if (-1 && 0) alert( 'second' ); -if (null || -1 && 1) alert( 'third' ); +if (-1 || 0) alert( 'Erster' ); +if (-1 && 0) alert( 'Zweiter' ); +if (null || -1 && 1) alert( 'Dritter' ); ``` diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/ifelse_task.svg b/1-js/02-first-steps/11-logical-operators/9-check-login/ifelse_task.svg index ca3e0aead..94f75ed7d 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/ifelse_task.svg +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/ifelse_task.svg @@ -1 +1,101 @@ -BeginCanceledCanceledWelcome!I don't know youWrong passwordWho's there?Password?CancelCancelAdminTheMasterOtherOther \ No newline at end of file + + + + + + + + + + + + Start + + + + + + Abbruch + + + + + + Abbruch + + + + + + Willkommen! + + + + + + Ich kenne Sie nicht! + + + + + + Falsches Passwort + + + + + + + + + Wer da? + + + + + + Passwort? + + + + + + Abbruch + + + + + + Abbruch + + + + + + Admin + + + + + + TheMaster + + + + + + + + Sonstige + + + + + + Sonstige + + + + + diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md index a30db7aae..3f8d2da9b 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md @@ -1,25 +1,25 @@ ```js run demo -let userName = prompt("Who's there?", ''); +let userName = prompt("Wer da?", ''); if (userName == 'Admin') { - let pass = prompt('Password?', ''); + let pass = prompt('Passwort?', ''); if (pass == 'TheMaster') { - alert( 'Welcome!' ); + alert( 'Willkommen!' ); } else if (pass == '' || pass == null) { - alert( 'Canceled' ); + alert( 'Abbruch' ); } else { - alert( 'Wrong password' ); + alert( 'Falsches Passwort' ); } } else if (userName == '' || userName == null) { - alert( 'Canceled' ); + alert( 'Abbruch' ); } else { - alert( "I don't know you" ); + alert( "Ich kenne Sie nicht!" ); } ``` -Note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable. +Achte auf die vertikalen Einrückungen innerhalb des `if`-Blocks, die syntaktisch nicht zwingend sind, aber den Code lesbarer machen. diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md index 290a52642..214a37e84 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md @@ -2,24 +2,24 @@ importance: 3 --- -# Check the login +# Login-Prüfung -Write the code which asks for a login with `prompt`. +Schreibe Code der mit `prompt` nach Anmeldedaten fragt. -If the visitor enters `"Admin"`, then `prompt` for a password, if the input is an empty line or `key:Esc` -- show "Canceled", if it's another string -- then show "I don't know you". +Gibt der Benutzer `"Admin"` ein, dann frage per `prompt` nach einem Passwort, ist die Eingabe eine Leerzeile oder `key:Esc` -- gib "Abbruch" aus, ist sie eine andere Zeichenkette -- dann gib aus "Ich kenne Sie nicht!". -The password is checked as follows: +Das Passwort wird wie folgt geprüft: -- If it equals "TheMaster", then show "Welcome!", -- Another string -- show "Wrong password", -- For an empty string or cancelled input, show "Canceled" +- Ist es "TheMaster", dann antworte mit "Willkommen!", +- Reagiere auf jede andere nicht-leere Zeichenkette mit der Anzeige -- "Falsches Passwort", +- Bei leerer Eingabzeile oder abgebrichener Eingabe zeige "Abbruch" an. -The schema: +Als Struktogramm: ![](ifelse_task.svg) -Please use nested `if` blocks. Mind the overall readability of the code. +Verwende eingerückte `if`-Blöcke und beachte die allgemeine Lesbarkeit des Codes. -Hint: passing an empty input to a prompt returns an empty string `''`. Pressing `key:ESC` during a prompt returns `null`. +Hinweis (Eingabeaufforderung): Eine leere Eingabe liefert die leere Zeichenkette `''`. Wird die Taste `key:ESC` betätigt, ist der Rückgabewert `null`. [demo] diff --git a/1-js/02-first-steps/11-logical-operators/article.md b/1-js/02-first-steps/11-logical-operators/article.md index 25f8ff7f5..1135c7603 100644 --- a/1-js/02-first-steps/11-logical-operators/article.md +++ b/1-js/02-first-steps/11-logical-operators/article.md @@ -1,24 +1,24 @@ -# Logical operators +# Logische Operatoren -There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT). +Es gibt drei logische Operatoren in JavaScript: `||` (ODER), `&&` (UND), `!` (NICHT). -Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type. +Obgleich sie "logisch" genannt werden, lassen sie sich auf Werte eines beliebigen Typs anwenden, nicht nur 'boolean'. Genauso kann ihr Ergebnis jedweden Typ haben. -Let's see the details. +Werfen wir einen Blick auf die Details. -## || (OR) +## || (ODER) -The "OR" operator is represented with two vertical line symbols: +Der Operator "ODER" wird mit zwei senkrechten Strichen repräsentiert: ```js result = a || b; ``` -In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are `true`, it returns `true`, otherwise it returns `false`. +In der klasssischen Programmierung dient das logische ODER nur zur Verarbeitung Boolescher Werte. Falls eines seiner Argumente `true` ist, liefert es `true`, andernfalls `false`. -In JavaScript, the operator is a little bit trickier and more powerful. But first, let's see what happens with boolean values. +In JavaScript ist der Operator mächtiger und in der Anwendung etwas diffiziler. Aber schauen wir uns zuerst an, was mit Booleschen Werten passiert. -There are four possible logical combinations: +Es gibt vier mögliche logische Kombinationen: ```js run alert( true || true ); // true @@ -27,21 +27,21 @@ alert( true || false ); // true alert( false || false ); // false ``` -As we can see, the result is always `true` except for the case when both operands are `false`. +Wir stellen fest, das Ergbenis ist immer `true` mit Ausnahme des Falls, daß beide Operanden `false` sind. -If an operand is not a boolean, it's converted to a boolean for the evaluation. +Falls ein Operand kein Boolescher Wert ist, wird er für die Auswertung in den Typ `boolean` konvertiert. -For instance, the number `1` is treated as `true`, the number `0` as `false`: +Beispielsweise wird die Zahl `1` als `true` interpretiert, die Zahl `0` als `false`: ```js run -if (1 || 0) { // works just like if( true || false ) - alert( 'truthy!' ); +if (1 || 0) { // Gleichwertig zu if( true || false ) + alert( 'Effektiv wahr!' ); } ``` -Most of the time, OR `||` is used in an `if` statement to test if *any* of the given conditions is `true`. +Meistens wird ODER `||` in einem `if`-Statement genutzt um zu prüfen, ob *irgendeine* der vorgegebenen Bedingungen `true` ist. -For example: +Beispiel: ```js run let hour = 9; @@ -49,91 +49,91 @@ let hour = 9; *!* if (hour < 10 || hour > 18) { */!* - alert( 'The office is closed.' ); + alert( 'Das Büro ist geschlossen.' ); } ``` -We can pass more conditions: +Wir können weitere Bedingungen ergänzen: ```js run let hour = 12; let isWeekend = true; if (hour < 10 || hour > 18 || isWeekend) { - alert( 'The office is closed.' ); // it is the weekend + alert( 'Das Büro ist geschlossen.' ); // Es ist Wochenende } ``` -## OR "||" finds the first truthy value +## ODER "||" findet den ersten effektiv wahren Wert -The logic described above is somewhat classical. Now, let's bring in the "extra" features of JavaScript. +Die oben beschriebene Logik ist sozusagen die klassische Variante. Nehmen wir jetzt die "speziellen" Features von JavaScript hinzu. -The extended algorithm works as follows. +Der erweiterte Algorithmus arbeitet wie folgt: -Given multiple OR'ed values: +Für mehrere mit ODER verknüpfte Werte: ```js result = value1 || value2 || value3; ``` -The OR `||` operator does the following: +arbeitet der Operator ODER `||` wie folgt: -- Evaluates operands from left to right. -- For each operand, converts it to boolean. If the result is `true`, stops and returns the original value of that operand. -- If all operands have been evaluated (i.e. all were `false`), returns the last operand. +- Wertet Operanden von links nach rechts aus. +- Konvertiert jeden Operand in einen Booleschen Wert. Ist das Ergebnis `true`, endet seine Auswertung und er gibt den ursprünglichen Wert dieses Operanden zurück. +- Sind alle Operanden ausgewertet ( d.h. alle waren `false` ), gibt er den letzten Operanden zurück. -A value is returned in its original form, without the conversion. +Ein Wert wird in seiner ursprünglichen Form zurückgegeben, ohne die Konvertierung. -In other words, a chain of OR `"||"` returns the first truthy value or the last one if no truthy value is found. +Mit anderen Worten, eine Folge von ODER `"||"` gibt den ersten effektiv wahren Wert zurück oder - sofern kein solcher gefunden wird - den letzten Wert. -For instance: +Beispiel: ```js run alert( 1 || 0 ); // 1 (1 is truthy) -alert( true || 'no matter what' ); // (true is truthy) +alert( true || 'egal was' ); // (true ist effektiv wahr) -alert( null || 1 ); // 1 (1 is the first truthy value) -alert( null || 0 || 1 ); // 1 (the first truthy value) -alert( undefined || null || 0 ); // 0 (all falsy, returns the last value) +alert( null || 1 ); // 1 (1 ist der erste effektiv wahre Wert) +alert( null || 0 || 1 ); // 1 (der erste effektiv wahre Wert) +alert( undefined || null || 0 ); // 0 (alle effektiv nicht wahr, gibt letzten Wert zurück) ``` -This leads to some interesting usage compared to a "pure, classical, boolean-only OR". +Das führt im Vergleich zu einem "reinen, klassischen, nur-Booleschen ODER" zu einigen interessanten Anwendungen: -1. **Getting the first truthy value from a list of variables or expressions.** +1. **Bestimme den ersten effektiv wahren Wert aus einer Liste von Variablen bzw. Ausdrücken.** - Imagine we have a list of variables which can either contain data or be `null/undefined`. How can we find the first one with data? + Nehmen wir an wir hätten eine Liste von Variablen, die entweder Daten beinhalten oder `null/undefined` sind. Wie finden wir die erste Variable mit Inhalt ? - We can use OR `||`: + Wir können ODER `||` nutzen: ```js run let currentUser = null; let defaultUser = "John"; *!* - let name = currentUser || defaultUser || "unnamed"; + let name = currentUser || defaultUser || "unbenannt"; */!* - alert( name ); // selects "John" – the first truthy value + alert( name ); // Gibt "John" aus – den ersten effektiv wahren Wert ``` - If both `currentUser` and `defaultUser` were falsy, `"unnamed"` would be the result. -2. **Short-circuit evaluation.** + Wären sowohl `currentUser` als auch `defaultUser` effektiv nicht wahr, lautete das Ergebnis `"unbenannt"`. +2. **Partielle Auswertung ( Short-circuit evaluation ).** - Operands can be not only values, but arbitrary expressions. OR evaluates and tests them from left to right. The evaluation stops when a truthy value is reached, and the value is returned. This process is called "a short-circuit evaluation" because it goes as short as possible from left to right. - - This is clearly seen when the expression given as the second argument has a side effect like a variable assignment. - - In the example below, `x` does not get assigned: + Operanden können nicht nur Werte sondern beliebige Ausdrücke sein. ODER führt deren Auswertung und Test in der Reihenfolge von links nach rechts aus. Die Auswertung endet, wenn ein effektiv wahrer Wert erreicht wird, und dieser Wert wird zurückgegeben. Dieses Prinzip nennt man "partielle Auswertung" ( "short-circuit evaluation" ), da sie von links nach rechts fortschreitend frühestmöglich abbricht und ggf. nicht alle Operanden berücksichtigt. + + Das kann man klar erkennen, wenn der als zweites Argument gegebene Ausdruck einen Seiteneffekt produziert, wie etwa eine Variablenzuweisung. + + Im folgenden Beispiel wird `x` nicht zugewiesen: ```js run no-beautify let x; *!*true*/!* || (x = 1); - alert(x); // undefined, because (x = 1) not evaluated + alert(x); // undefined, denn (x = 1) wird nicht ausgewertet ``` - If, instead, the first argument is `false`, `||` evaluates the second one, thus running the assignment: + Ist hingegen das erste Argument `false`, wertet `||` das Zweite aus und führt die Zuweisung durch: ```js run no-beautify let x; @@ -143,21 +143,21 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl alert(x); // 1 ``` - An assignment is a simple case. There may be side effects, that won't show up if the evaluation doesn't reach them. - - As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. If it's false, the second one is evaluated. + Eine Zuweisung ist ein einfacher Fall. Es kann Seiteneffekte geben, deren Ausbleiben sich nicht unmittelbar manifestiert, wenn die Auswertung sie nicht erreicht. - Most of time, it's better to use a "regular" `if` to keep the code easy to understand, but sometimes this can be handy. + Wie wir erkennen ist ein solcher Anwendungsfall eine verkürzte Fallunterschiedung mit `if`. Der erste Operand wird in einen Booleschen Wert konvertiert. Ist er `false`, wird der zweite Operand ausgewertet. + + Meistens ist ein "normales" `if` vorzuziehen, um den Code möglichst verständlich zu halten, manchmal kann die Kurzvariante aber ganz praktisch sein. -## && (AND) +## && (UND) -The AND operator is represented with two ampersands `&&`: +Der Operator "UND" wird mit zwei kaufmännischen Und-Symbolen (Ampersands) repräsentiert: ```js result = a && b; ``` -In classical programming, AND returns `true` if both operands are truthy and `false` otherwise: +In der klasssischen Programmierung gibt UND `true` zurück, wenn beide Operanden effektiv wahr sind, ansonsten `false`: ```js run alert( true && true ); // true @@ -166,138 +166,138 @@ alert( true && false ); // false alert( false && false ); // false ``` -An example with `if`: +Ein beispiel mit `if`: ```js run let hour = 12; let minute = 30; if (hour == 12 && minute == 30) { - alert( 'The time is 12:30' ); + alert( 'Es ist 12:30 Uhr' ); } ``` -Just as with OR, any value is allowed as an operand of AND: +Genau wie bei ODER ist jeder Wert als Operand von UND zulässig: ```js run -if (1 && 0) { // evaluated as true && false - alert( "won't work, because the result is falsy" ); +if (1 && 0) { // Interpretiert als true && false + alert( "Nie zu sehen, denn das Ergebnis war effektiv nicht wahr." ); } ``` -## AND "&&" finds the first falsy value +## UND "&&" findet den ersten effektiv nicht wahren Wert -Given multiple AND'ed values: +Für mehrere mit UND verknüpfte Werte: ```js result = value1 && value2 && value3; ``` -The AND `&&` operator does the following: +Der Operator UND `&&` arbeitet wie folgt: -- Evaluates operands from left to right. -- For each operand, converts it to a boolean. If the result is `false`, stops and returns the original value of that operand. -- If all operands have been evaluated (i.e. all were truthy), returns the last operand. +- Wertet Operanden von links nach rechts aus. +- Konvertiert jeden Operand in einen Booleschen Wert. Ist das Ergebnis `false`, endet seine Auswertung und er gibt den ursprünglichen Wert dieses Operanden zurück. +- Sind alle Operanden ausgewertet ( d.h. alle waren effektiv wahr ), gibt er den letzten Operanden zurück. -In other words, AND returns the first falsy value or the last value if none were found. +Mit anderen Worten, UND gibt den ersten effektiv nicht wahren Wert zurück oder - sofern kein solcher gefunden wird - den letzten Wert. -The rules above are similar to OR. The difference is that AND returns the first *falsy* value while OR returns the first *truthy* one. +Die Regeln sind ähnlich denen für ODER. Der Unterschied besteht darin, daß UND den ersten *effektiv nicht wahren* Wert zurückgibt, während es bei ODER der erste *effektiv whare* ist. -Examples: +Beispiele: ```js run -// if the first operand is truthy, -// AND returns the second operand: +// Ist der erste Operand effektiv wahr, +// gibt UND den zweiten Operanden zurück: alert( 1 && 0 ); // 0 alert( 1 && 5 ); // 5 -// if the first operand is falsy, -// AND returns it. The second operand is ignored +// Ist der erste Operand effektiv nicht wahr, +// gibt ihn UND zurück. Der zweite Operand wird ignoriert. alert( null && 5 ); // null alert( 0 && "no matter what" ); // 0 ``` -We can also pass several values in a row. See how the first falsy one is returned: +Wir können auch mehrere Werte am Stück verknüpfen. Hier sehen wir, wie der erste effektiv nicht wahre Wert zurückgegeben wird. ```js run alert( 1 && 2 && null && 3 ); // null ``` -When all values are truthy, the last value is returned: +Wenn alle Werte effektiv wahr sind, wird der letzte Wert zurückgegeben: ```js run -alert( 1 && 2 && 3 ); // 3, the last one +alert( 1 && 2 && 3 ); // 3, der letzte Wert ``` -````smart header="Precedence of AND `&&` is higher than OR `||`" -The precedence of AND `&&` operator is higher than OR `||`. +````smart header="Präzedenz von UND `&&` ist höher als von ODER `||`" +Die Präzedenz der Operators UND `&&` ist höher als die von ODER `||`. -So the code `a && b || c && d` is essentially the same as if the `&&` expressions were in parentheses: `(a && b) || (c && d)`. +Der Code `a && b || c && d` verhält sich daher i.w. so, als ob die Ausdrücke mit `&&` in Klammern gesetzt würden: `(a && b) || (c && d)`. ```` -Just like OR, the AND `&&` operator can sometimes replace `if`. +So wie ODER kann auch der Operator UND `&&` manchmal eine Fallunterscheidung (`if`) ersetzen. -For instance: +Beispiel: ```js run let x = 1; -(x > 0) && alert( 'Greater than zero!' ); +(x > 0) && alert( 'Größer 0!' ); ``` -The action in the right part of `&&` would execute only if the evaluation reaches it. That is, only if `(x > 0)` is true. +Die Aktion im rechten Zweig von `&&` kommt nur zur Ausführung, wenn die Auswertung des Ausdrucks sie erreicht. Also nur falls `(x > 0)` wahr ist. -So we basically have an analogue for: +Damit haben wir im Prinzip ein Pendant zu: ```js run let x = 1; if (x > 0) { - alert( 'Greater than zero!' ); + alert( 'Größer 0!' ); } ``` -The variant with `&&` appears shorter. But `if` is more obvious and tends to be a little bit more readable. +Die Variante mit `&&` wirkt knapper. Aber `if` ist leichter zu identifizieren und tendenziell eines kleines bisschen verständlicher. -So we recommend using every construct for its purpose: use `if` if we want if and use `&&` if we want AND. +Daher empfehlen wir, jedes Konstrukt gemäß seinem Zweck einzusetzen: `if` nutzen, wenn wir eine Fallunterscheidung haben wollen, `&&` für die logische Operation UND. -## ! (NOT) +## ! (NICHT) -The boolean NOT operator is represented with an exclamation sign `!`. +Der Boolsche Operator NICHT wird durch ein Ausrufezeichen `!` repräsentiert -The syntax is pretty simple: +Die Syntax ist einfach genug: ```js result = !value; ``` -The operator accepts a single argument and does the following: +Der Operator akzeptiert ein einzelnes Argument und erledigt folgendes: -1. Converts the operand to boolean type: `true/false`. -2. Returns the inverse value. +1. Konvertiert den Operand in einen Booleschen Wert: `true/false`. +2. Gibt den negierten Wert zurück. -For instance: +Beispiel: ```js run alert( !true ); // false alert( !0 ); // true ``` -A double NOT `!!` is sometimes used for converting a value to boolean type: +Ein doppeltes NICHT `!!` verwendet man gelegentlich zur Konvertierung in einen Booleschen Wert: ```js run -alert( !!"non-empty string" ); // true +alert( !!"Nicht-leere Zeichenkette" ); // true alert( !!null ); // false ``` -That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion. +Das erste NICHT konvertiert den Wert in einen Booleschen Wert und gibt den negierten Wert zurück, während das zweite NICHT ein weiteres Mal negiert. Als Ergebnis erhalten wir die elementare Konvertierung eines Werts in einen Booleschen Wert. -There's a little more verbose way to do the same thing -- a built-in `Boolean` function: +Eine etwas ausführlichere Notation bewerkstelligt das gleiche -- die vordefinierte Funktion `Boolean`: ```js run -alert( Boolean("non-empty string") ); // true +alert( Boolean("Nicht-leere Zeichenkette") ); // true alert( Boolean(null) ); // false ``` -The precedence of NOT `!` is the highest of all logical operators, so it always executes first, before `&&` or `||`. +Die Präzedenz von NOT `!` ist die höchste aller logischen Operatoren, damit wird sie immer zuerst ausgeführt, vor `&&` oder `||`.