# Convert a Negative Number to Positive in JavaScript

Fri Oct 22 20211 min read

## Convert a Negative Number to Positive#

To convert a negative number to a positive number, call the Math.abs() method passing it the number as a parameter, e.g. Math.abs(-10). When supplied with a negative number, the Math.abs method is guaranteed to return a positive number.

index.js
console.log(Math.abs(-5)); // ๐๏ธ 5 console.log(Math.abs(-5.5)); // ๐๏ธ 5.5 console.log(Math.abs(-0)); // ๐๏ธ 0 console.log(Math.abs(1.5)); // ๐๏ธ 1.5 console.log(Math.abs('-3.5')); // ๐๏ธ 3.5

We used the Math.abs method to convert negative to a positive numbers.

The only parameter the method takes is the number we want to get the absolute value of.

The method returns the absolute value of a number, in other words it returns the number's distance from zero.

The Math.abs method returns the provided number if it's positive or zero and the negation of the number if it's negative.

When you pass a negative number as a parameter to the Math.abs method, it always returns a positive number.

You might see examples online, where the number is prefixed with a minus - to invert its sign.

index.js
const num1 = -5; const num2 = 5; console.log(-num1); // ๐๏ธ 5 console.log(-num2); // ๐๏ธ -5

However, all this does is, it inverts the number's sign, it doesn't necessarily convert negative numbers to positive, it goes both ways.

On the other hand, the Math.abs method is guaranteed to give you a positive number, no matter if you pass it a positive or a negative number.