Math.abs()
La funzione Math.abs()
ritorna il valore assoluto di un numero, ovvero
Sintassi
Math.abs(x)
Parametri
x
- Un numero.
Valore di ritorno
Il valore assoluto del numero passato come parametro.
Descrizione
Siccome abs()
è un metodo statico di Math
, viene sempre utilizzato come Math.abs()
piuttosto che come metodo di un oggetto Math
creato in precedenza (Math
non è un costruttore).
Esempi
Comportamento di Math.abs()
Passando un oggetto vuoto, un array con più di un elemento, una stringa non numerica o una variabile undefined
/empty il valore di ritorno sarà NaN
. Passando null
, una stringa vuota o un array vuoto, il valore di ritorno sarà 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
Specifiche
Specifica | Stato | Commento |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Definizione iniziale. Implementata in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262) The definition of 'Math.abs' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.abs' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'Math.abs' in that specification. |
Living Standard |
Compatibilità con i Browser
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.