Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert a negative number to a positive one in JavaScript?
This tutorial will teach us to convert negative numbers to positive ones. Sometimes, programmers need to perform operations with the unsigned value of a number, and in such cases, they need to convert negative numbers to positive numbers.
We will explore three different methods to convert negative numbers to positive numbers in JavaScript:
Using the Math.abs() Method
Multiplying the negative number with -1
Using the Bitwise Not Operator
Using the Math.abs() Method
The Math.abs() method is the most commonly used approach. It takes a value as a parameter and returns the absolute (unsigned) value.
Syntax
let number = -10; let result = Math.abs(number);
Parameters
number ? It can be negative or positive number values which we need to convert to unsigned number.
Example
In the below example, we have converted different negative and positive numbers to positive ones using the Math.abs() method.
<html>
<head>
</head>
<body>
<h2> Converting the negative number to positive number in JavaScript. </h2>
<h4> Using <i> Math.abs() </i> method to convert different negative values to positive. </h4>
<div id="number1"> </div>
</body>
<script>
var number1 = document.getElementById("number1");
let number = -234;
// using the Math.abs() for different values
number1.innerHTML = "Math.abs( -234 ) = " + Math.abs(number) + " <br/> ";
number1.innerHTML += "Math.abs( 234 ) = " + Math.abs(234) + " <br/> ";
number1.innerHTML += "Math.abs( -1235.462 ) = " + Math.abs(-1235.462) + " <br/> ";
</script>
</html>
Math.abs( -234 ) = 234 Math.abs( 234 ) = 234 Math.abs( -1235.462 ) = 1235.462
Multiplying the Negative Number with -1
The second approach is straightforward mathematical logic. We check if the number is negative and multiply it by -1 to make it positive. If the number is already positive, we keep it unchanged.
Syntax
let number = -543; let positive = number * -1;
Example
In the example below, we have implemented the above approach using a ternary operator to check whether the number is less than zero.
<html>
<head>
</head>
<body>
<h2> Converting the negative number to positive number in JavaScript. </h2>
<h4> <i> Multiply number with -1 </i> to convert negative number to positive. </h4>
<div id="number1"> </div>
</body>
<script>
var number1 = document.getElementById("number1");
function getPositive(number) {
// if number is less than zero multiply with -1, otherwise returns as it is
return number < 0 ? number * -1 : number;
}
number1.innerHTML = "-43 => " + getPositive(-43) + " <br/> ";
number1.innerHTML += "43 => " + getPositive(43) + " <br/> ";
</script>
</html>
-43 => 43 43 => 43
Using the Bitwise Not Operator
This approach uses the Bitwise Not (~) operator to convert negative numbers to positive. The computer represents numbers in binary format and uses two's complement for negative numbers. The bitwise NOT operator inverts all bits, and adding 1 gives us the absolute value.
Syntax
let number = -10; let result = ~(number) + 1;
Example
In the below example, we take the two's complement of the negative number and add 1 to get the positive value.
<html>
<head>
</head>
<body>
<h2> Converting the negative number to positive number in JavaScript. </h2>
<h4> Using the <i> Bitwise Not (~) </i> operator to convert negative number to positive. </h4>
<div id="number1"> </div>
</body>
<script>
var number1 = document.getElementById("number1");
function getPositive(number) {
// if number is less than zero, take two's complement and add 1 to it.
return number < 0 ? ~number + 1 : number;
}
number1.innerHTML = "312 => " + getPositive(312) + " <br/> ";
number1.innerHTML += "-5 => " + getPositive(-5) + " <br/> ";
</script>
</html>
312 => 312 -5 => 5
Comparison
| Method | Ease of Use | Performance | Readability |
|---|---|---|---|
| Math.abs() | Very Easy | Good | Excellent |
| Multiply by -1 | Easy | Very Good | Good |
| Bitwise NOT | Complex | Excellent | Poor |
Conclusion
The Math.abs() method is the recommended approach for most use cases due to its simplicity and readability. The multiplication method offers good performance, while the bitwise approach provides the fastest execution but requires deeper understanding of binary operations.
