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 some operation with the unsigned value of the number, and in such a case, they need to convert the negative numbers to positive numbers.

We will go through the various methods to convert a negative number to a positive number in this tutorial.

  • 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 JavaScript math library method. The Math.abs() method takes a value as a parameter and returns the unsigned value.

Syntax

Users can follow the below syntax to use the Math.abs() method.

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 the different negative and positive numbers to positive ones using the Math.abs() method. Users will learn to use the Math.abs() method using the below example.

<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>

In the above output, users can see that positive value remains same, while negative values converted to the positive ones.

Multiplying the Negative Number with -1

The second approach to converting a negative to a positive number is straightforward. We will check for the number using the if-else statement. If the number is less than zero, which means negative, we will multiply it by -1. Otherwise, we will keep the number as it is.

Syntax

let number  = -543;
let positive = number * -1;

Example

In the example below, we have implemented the above approach. We have created the function to get the positive numbers from negative numbers. In the function, we have added the ternary operator to check whether the number is less than zero. If the number will less than 0, the ternary operator returns the number after multiplying with -1.

<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>

Using the Bitwise Not Operator

In this approach, we will use the Bitwise Not (~) operator to convert the signed number to an unsigned number. The process of converting a negative to a positive number is tricky if we try to understand the language of computers.

The computer understands the language of binary digits only. So, the computer converts the number to binary, takes two’s complement, and adds 1 to the number to convert a negative number to a positive.

We will use the Bitwise ~ operator to take a two’s complement of a number and add 1 to the number. 

Syntax

Users can follow the below syntax for this method.

let number = -10;
let result = ~( number ) + 1;

Example

In the below example, we have taken the two’s complement of the negative number and added 1 to the number.

<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>

The first approach uses the Math.abs() method to convert the negative values to positive ones. Almost programmers know the second approach. The third approach is the tricky approach; which users can understand if they have good knowledge of binary numbers. Also, the third approach is the fastest approach among all three.

Updated on: 10-Aug-2022

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements