Table of Content
Some numbers come from user input and other come from math operations. Sometimes, these numbers need to stay positive. The JavaScript Math.abs() function gives the absolute value.
It changes negative numbers to positive and keeps positive numbers the same.
Understand Math abs in JavaScript
The Math.abs() function belongs to the built-in Math object. It takes one value and gives you the absolute version of that value. It does not change the original number.
This function removes the sign of a number. It does not check where the number came from. It only cares about the final value. Negative numbers become positive. Zero stays zero. Positive numbers stay the same.
Here is the syntax:
Math.abs(number)You add one number or value that becomes a number. The function gives back one number. That number has no negative sign.
Here is a quick example:
const result = Math.abs(-8);
console.log(result); // 8This code passes -8 into Math.abs(). The function removes the minus. It returns 8. The console prints 8.
The function takes different types of values. You can pass a number. You can pass a string like "5" that turns into a number or pass an expression like 3 - 10. JavaScript turns the input into a number. If it fails, the function gives NaN.
Here are the use cases:
- Show a distance between two values.
- Compare sizes without the sign.
- Fix user input that has a minus by mistake.
- Build game logic that counts movement or damage.
- Handle math that can flip between positive and negative.
Examples of JavaScript Math.abs
Use Math.abs with positive and negative numbers:
console.log(Math.abs(4)); // 4
console.log(Math.abs(-4)); // 4The first line keeps the value. The second line flips the sign.
Use Math.abs in basic arithmetic operations:
const a = 5 - 10;
const b = Math.abs(a);
console.log(b); // 5This code runs 5 - 10 and gets -5. Math.abs() changes it to 5.
Convert values from user input:
const input = "-12";
const value = Math.abs(Number(input));
console.log(value); // 12This example turns a string into a number. The function removes the minus.
Browser Support for JavaScript Math.abs()
You can use Math.abs() in almost every browser. It works the same way on each one.
Desktop browsers that support it:
- Chrome (version 4 or later)
- Firefox (version 2 or later)
- Safari (version 3.1 or later)
- Edge (version 12 or later)
- Opera (version 10 or later)
- Internet Explorer (version 6 or later)
Mobile browsers that support it:
- Chrome for Android
- Safari on iPhone (version 3.2 or later)
- Samsung Internet
- Opera Mini
- Opera Mobile
- UC Browser for Android
- Android Browser
- Firefox for Android
- QQ Browser
- Baidu Browser
You do not need to check for support. The function works in both old and new browsers without problems.
Wrapping Up
In this article, you learned what the JavaScript Math.abs() function does. You saw how it handles signs in numbers.
Here is a quick recap:
- Math.abs() comes from the Math object.
- It gives the absolute value.
- It works with numbers, strings, and expressions.
- It does not change the input.
- It gives a clean positive value or zero.
- It gives
NaNif the input fails to convert.
You can use this function in many types of logic. It helps clean up and fix numbers that carry signs.
FAQs
What is Math.abs in JavaScript?
What Does Math.abs() Do?
Can Math.abs Work with BigInt?
What Happens If You Pass No Argument?
How Does Math.abs Handle Infinity?
Similar Reads
The this keyword links code, context, and object reference in JavaScript. You can use it to refer to the owner…
Object References in JavaScript mean that variables do not store full objects but only their references in memory. What is…
JavaScript Objects hold values as key and value pairs, and they help you store and access data. Understand the JavaScript…
Arrow function gives you short code and keeps this in the right scope in JavaScript. Understand the Arrow Function in…
Math.random() gives a number between 0 and 1 in JavaScript. It helps create random values for colors or other data.…
String Operators in JavaScript help you work with text. You can join, build, or change strings. Most of what you…
Unary operators in JavaScript work with only one value. They can change, test, or change the type of that value,…
JavaScript Math.tan() finds the tangent of an angle in radians. You use it when you need to solve angle-based math.…
JavaScript Ninja Code points to ways that help a person write code that runs fast and stays easy to read.…
Some numbers do not stay whole. You get decimal parts when you divide or calculate prices. You may need to…