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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
To practice and improve yourself in the interview, you can also check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews.
Happy Learning!!