欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

介绍JavaScript中Math.abs()方法的使用

程序员文章站 2023-11-27 22:00:04
 这个方法返回一个数字的绝对值。 语法 math.abs( x ) ; 下面是参数的详细信息:   &nb...

 这个方法返回一个数字的绝对值。
语法

math.abs( x ) ;

下面是参数的详细信息:

  •     x : 一个数字

返回值:

  • 返回一个数字的绝对值

例子:

<html>
<head>
<title>javascript math abs() method</title>
</head>
<body>
<script type="text/javascript">

var value = math.abs(-1);
document.write("first test value : " + value ); 
 
var value = math.abs(null);
document.write("<br />second test value : " + value ); 

var value = math.abs(20);
document.write("<br />third test value : " + value ); 

var value = math.abs("string");
document.write("<br />fourth test value : " + value ); 
</script>
</body>
</html>

这将产生以下结果:

first test value : 1
second test value : 0
third test value : 20
fourth test value : nan