User Tools

Site Tools


programming:javascript:usage

This is an old revision of the document!


Funktionen

Objekte

Ein Objekt gruppiert mehrere Werte zusammen:

const objektEins = {
   name: 'Hans',
   alter: 27
   };
   
console.log(objektEins);

{name: 'Hans', alter: 27}

Einfache bedingte Ausdrücke

Allgemein bei return:
predicate ? consequent-expression : alternative-expression

predicate: x >= 0
consequent-expression: x
alternative-expression: -x

function abs(x) {
  return x >= 0 ? x : - x;
}

Mehrfach bedingte Ausdrücke

p1, p2, … pn: Prädikate
e1, e2, … en: Ausdrücke
final-alternative-expression: letzter Ausdruck fall alle anderen nicht zutreffen.

p1 ? e1 : p2 ? e2: pn ? en : final-alternative-expression

function abs(x) {
  return x > 0
         ? x
         : x === 0
         ? 0
         : -x;
}

zusammengesetzte Bedingungen

expression1 && expression2 → und
entspricht: expression1 ? expression2 : false

> function test_zahl(z){ return z>0 && z<11 ? z : console.log("falsch")}

> test_zahl(2)
2
> test_zahl(22)
falsch
> function test2_zahl(z){ return z>0 ? z<11 : console.log("falsch")}

> test2_zahl(2)
true
> test2_zahl(22)
false
> test2_zahl(-2)
falsch

expression1 || expression2 → oder
entspricht: expression1 ? true : expression2

! expression → nicht (negation)

programming/javascript/usage.1701856572.txt.gz · Last modified: 2023/12/06 10:56 by ms

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki