JavaScript

Math.abs()

 編集:2022.08.07 by T.A.

Math.abs(x)
x 数を現す値。
戻り値

与えられた数値の絶対値。

Math.abs() の動作

空のオブジェクト、複数のメンバーを持つ配列、数値でない文字列、undefined、空の変数を渡すと、NaN を返します。null を渡すと空文字列を返し、空の配列は 0 を返します。

D=Math.abs('-1');	//=1
D=Math.abs(-2);	//=2
D=Math.abs(null);	//=0
D=Math.abs('');	//=0
D=Math.abs([]);	//=0
D=Math.abs([2]);	//=2
D=Math.abs([1,2]);	//=NaN
D=Math.abs({});	//=NaN
D=Math.abs('string');	//=NaN
D=Math.abs();	//=NaN

テスト