Home » JavaScript Math abs() Method

JavaScript Math abs() Method

by Online Tutorials Library

JavaScript Math abs() method

The JavaScript math abs() method returns an absolute value of a given number. The abs() is a static method of Math.

Syntax

The abs() method is represented by the following syntax:

Parameter

num – A number.

Return

An absolute value of a number.

JavaScript Math abs() method example

Here, we will understand abs() method through various examples.

Example 1

Let’s see an example to print the absolute value of a number.

Test it Now

Output:

4  7.8  4  

Example 2

Let’s see some cases where abs() method returns 0.

Test it Now

Output:

0  0  0  

Example 3

Let’s see some cases where abs() method returns NaN.

Test it Now

Output:

NaN  NaN  NaN  

Example 4

Let’s understand the abs() method with your own test cases.

Test it Now

Next TopicJavaScript Math

You may also like