Bibliotheksreferenz

Absolutbetrag

Der Betrag zeigt dir an, wie weit eine Zahl von null entfernt ist.

Math.abs

Ein einfaches Beispiel

Am Anfang−3
Ergebnis3
lesson.js
console.log(Math.abs(-3));
Ausgabe
3
Anzeigen

Beispiele aus dem Alltag

Es ist −6 Grad draußen. Wie viele Grad unter Null ist das?

Am Anfang−6 → 0
Ergebnis6
lesson.js
const temperature = -6;
console.log(Math.abs(temperature));
Ausgabe
6

Das Ziel ist 15 Punkte, die Punktzahl beträgt 12. Wie weit sind sie voneinander entfernt?

Am Anfang12 − 15
Ergebnis3
lesson.js
const score = 12;
const target = 15;
console.log(Math.abs((score - target)));
Ausgabe
3

Ein Aufzug fährt von der Etage 5 auf die Etage 2. Wie viele Stockwerke legt er zurück?

Am Anfang5 → 2
Ergebnis3
lesson.js
const start = 5;
const finish = 2;
console.log(Math.abs((finish - start)));
Ausgabe
3

Probiere es hier mit kleinen positiven und negativen Zahlen. Das Ergebnis ist der Abstand zur Null.

Beispiel ausführen
Terminal
node lesson.js
Offizielle Dokumentation