Home>Article>Web Front-end> How to output absolute value in JavaScript

How to output absolute value in JavaScript

藏色散人
藏色散人 Original
2021-07-13 16:18:23 2354browse

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)

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);

Output results:

7.25 7.25 0 NaN 5

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!

Statement:
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
Previous article:What does css consist of? Next article:What does css consist of?