Table of contents
1.
Introduction
2.
Math in JavaScript
3.
Math.abs() in JavaScript
3.1.
Syntax
3.2.
Example of Math abs JavaScript
4.
Use Cases of Math abs
5.
Frequently Asked Questions
5.1.
What does Math abs JavaScript do?
5.2.
What is the return value of the Math abs JavaScript if the parameter is a negative number?
5.3.
What happens if you pass a non-numeric value as the parameter to the Math abs JavaScript?
5.4.
What happens if you pass an empty parameter to the Math abs JavaScript?
6.
Conclusion
Last Updated: Aug 13, 2025
Easy

Math abs JavaScript

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello Ninjas, do you know what Math abs JavaScript is? Have you ever thought about why to use the Math abs() method in JavaScript? If not, and you want to know about it, then don't worry, ninjas. Coding Ninjas got your back. We will clear all your doubts.

math abs JavaScript

In this article, we will discuss about Math abs JavaScript. We will discuss what it is and how to use it. Before moving on to the main topic, let us understand what math is that we use before writing the abs() method.

Math in JavaScript

In Javascript, Math is a built-in object. It helps us to provide different functions to perform mathematical operations easily. It consists of different types of functions, such as

  • Trigonometry
     
  • Logarithms
     
  • Basic Arithmetic
     
  • Random number generator
     
  • Exponential Methods
     

Math is very helpful, and it makes it easy for us to perform mathematical calculations. Suppose we want to make a calculator in JavaScript. Then, we can use Math. It makes it easy to write simpler code rather than typing and doing calculations manually.

There are some examples of functions that are available in Math:

 functions available in Math

Along with these functions, one function is also there in JavaScript using Math, i.e., Math.abs(). Let us understand what it is.

Math.abs() in JavaScript

Math abs JavaScript is a built-in function. It returns the absolute value of a number. The absolute value of a number means its distance from zero on the number line. This value doesn't care about its sign, whether it is positive or negative. In simple words, the absolute value is the magnitude of the number. This is always a positive value. 

For example, the absolute value of -19 is 19. Because 19 units separate -19 from 0 on the number line. Similarly, the absolute value of 19 is also 19. Because 19 units separate 19 from 0 on the number line.

Let us look at the syntax of Math abs JavaScript.

Syntax

The syntax of Math abs JavaScript is very simple. You can write it as mentioned below:

Math.abs(value);


Note: Math abs JavaScript takes a single parameter. This parameter can be a number or an expression that evaluates to a number. If we pass a parameter in this function, it will return a positive value.

Example of Math abs JavaScript

Let us look at an example of Math abs JavaScript.

let value=-10
console.log("Before finding absolute value: "+value)
console.log("Absolute value: "+Math.abs(value))

 

Output

Before finding absolute value: -10
Absolute value: 10

 

Let us discuss some use cases of Math abs JavaScript.

Use Cases of Math abs

There are some use cases of Math abs JavaScript. Let us discuss them.

Use Case 1: If you pass a string in the place of value, this will return NaN. Before returning NaN, it will try to convert the string to a number first. If it fails to convert it into a number, then only it will return NaN. Let us understand this with the help of an example.

let value="10";
let str="Hello Ninjas";
console.log(Math.abs(value));
console.log(Math.abs(str));

 

Output

10
NaN

 

Use Case 2: If you pass an [] or null value, this will return 0. It will consider null as a falsy. That's why this will evaluate null and [] as 0. Let us understand this with the help of an example.

let value=null;
let arr=[];
console.log(Math.abs(value));
console.log(Math.abs(arr));

 

Output

0
0


Use Case 3: If you leave blank parentheses of Math abs JavaScript, it will return NaN. Because the function requires a parameter. This parameter will represent the value you want to calculate the absolute value of. Let us understand this with help of an example.

console.log(Math.abs());

 

Output

NaN

Frequently Asked Questions

What does Math abs JavaScript do?

The Math abs JavaScript returns the absolute value of a number. It takes one parameter. This parameter is the number whose absolute value you want to calculate.

What is the return value of the Math abs JavaScript if the parameter is a negative number?

This function always returns a positive number. It doesn't consider any of the signs of the input parameter. If the parameter is a negative number, the function returns the absolute value of that number.

What happens if you pass a non-numeric value as the parameter to the Math abs JavaScript?

If you pass a non-numeric value, such as a string or an array, as the parameter to the Math abs JavaScript, it will return NaN (Not a Number).

What happens if you pass an empty parameter to the Math abs JavaScript?

If you leave blank parentheses of the Math abs function, it will return NaN. Because the function requires a parameter representing the value you want to calculate the absolute value of.

Conclusion

In this article, we have discussed the concept of Math abs JavaScript. We have also discussed the examples by using the Math abs JavaScript method. You can check out our other blogs to enhance your knowledge:

We hope this blog helped you to understand the Math abs JavaScript method. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews

Happy Learning!!

Live masterclass