Math.abs()
La fonction Math.abs() retourne la valeur absolue d'un nombre, c'est-à-dire
Syntaxe
Math.abs(x);
Paramètres
x-
Un nombre.
Valeur absolue
La valeur absolue du nombre passé en argument.
Description
abs est une méthode statique de l'objet Math et doit toujours être utilisée avec la syntaxe Math.abs().
Exemples
Utiliser Math.abs()
Si la méthode est utilisée avec une chaîne de caractères non numérique, avec un tableau à plus d'un élément, sans paramètre ou avec undefined, la valeur retournée sera NaN. Si elle est utilisée avec null, la fonction renverra 0.
Math.abs('-1'); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(''); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1,2]); // NaN
Math.abs({}); // NaN
Math.abs("string"); // NaN
Math.abs(); // NaN
Spécifications
| Spécification | État | Commentaires |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Définition initiale. Implémentée avec JavaScript 1.0. |
| ECMAScript 5.1 (ECMA-262) La définition de 'Math.abs' dans cette spécification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) La définition de 'Math.abs' dans cette spécification. |
Standard | |
| ECMAScript (ECMA-262) La définition de 'Math.abs' dans cette spécification. |
Standard évolutif |
Compatibilité des navigateurs
BCD tables only load in the browser