Home > Web Front-end > Front-end Q&A > How to output absolute value in JavaScript

How to output absolute value in JavaScript

藏色散人
Release: 2021-07-13 16:18:23
Original
2491 people have browsed it

In JavaScript, the absolute value can be output through the abs method. The syntax is "Math.abs(x)". The parameter x represents a numerical value, and the return value is the absolute value of Number x.

How to output absolute value in JavaScript

The operating environment of this article: Windows 7 system, javascript1.8.5, Dell G3 computer.

How to output absolute value in JavaScript?

abs() method returns the absolute value of a number.

Syntax

Math.abs(x)
Copy after login

Parameter values

x Required. Must be a numeric value.

Return value

The absolute value of Number x. Returns NaN if x is not a number, or 0 if x is null.

Example

In this example, I will get the absolute values ​​of different types of arrays:

var a=Math.abs(7.25);
var b=Math.abs(-7.25);
var c=Math.abs(null);
var d=Math.abs("Hello");
var e=Math.abs(2+3);
Copy after login

Output results:

7.25
7.25
0
NaN
5
Copy after login

Recommended learning:《javascript advanced tutorial

The above is the detailed content of How to output absolute value in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template