In JavaScript, a negative number can be converted to its positive equivalent using arithmetic operations or built-in methods.
- Multiplying a negative number by -1 converts it to positive.
- Math.abs() directly returns the absolute value of a number.
- The unary minus (-) operator can be used to change the sign of a negative number.
- Bitwise operations can also convert negative integers, but they are less suitable for general-purpose number handling.
- Math.sqrt() with Math.pow() can obtain the positive magnitude of a number.
Approach 1: Using the Multiplication Operator (*)
A negative number can be converted to a positive number by multiplying it by -1. Before multiplying, check whether the number is negative to avoid changing an already positive number.
Working:
- Check whether the number is less than 0.
- If it is negative, multiply it by -1.
- Return the resulting positive number.
Syntax:
if (number < 0) {
number = number * -1;
}
function convertToPositive(number) {
if (number < 0) {
number = number * -1;
}
return number;
}
const number1 = -10;
const number2 = 5;
console.log(convertToPositive(number1));
console.log(convertToPositive(number2));
Output
10 5
Approach 2: Using the Math.abs() Method
The Math.abs() method returns the absolute value of a number. Therefore, it can be used to convert a negative number into its positive equivalent.
Working:
- Pass the number to Math.abs().
- If the number is negative, its positive equivalent is returned.
- If the number is already positive, it remains unchanged.
Syntax:
Math.abs(number)const number1 = -30;
const number2 = 15;
console.log(Math.abs(number1));
console.log(Math.abs(number2));
Output
30 15
Approach 3: Using the Unary Minus (-) Operator
The unary minus operator can be used to change the sign of a number. By checking whether the number is negative, we can negate it only when necessary.
Syntax:
number < 0 ? -number : numberfunction convertToPositive(number) {
return number < 0 ? -number : number;
}
const number1 = -10;
const number2 = 5;
console.log(convertToPositive(number1));
console.log(convertToPositive(number2));
Output
10 5
Approach 4: Using Bitwise Operators
The bitwise NOT (~) operator can be used with +1 to obtain the positive equivalent of a negative integer. This works because JavaScript represents bitwise operands as signed 32-bit integers.
Syntax:Â
number < 0 ? ~number + 1 : numberfunction convertToPositive(number) {
return number < 0 ? ~number + 1 : number;
}
const number1 = -10;
const number2 = 5;
console.log(convertToPositive(number1));
console.log(convertToPositive(number2));
Output
10 5
Note: Bitwise operators convert their operands to signed 32-bit integers, so this approach is not recommended for general-purpose numbers, especially numbers outside the 32-bit integer range.
Approach 5: Using Math.sqrt() and Math.pow()
The Math.pow() method can square a number, making the result positive. Then, Math.sqrt() can be used to obtain its positive square root.
Working:
- Square the number using Math.pow(number, 2).
- The squared value is always non-negative.
- Use Math.sqrt() to obtain the positive magnitude.
Syntax:
Math.sqrt(Math.pow(number, 2))function convertToPositive(number) {
return Math.sqrt(Math.pow(number, 2));
}
const negativeNumber = -5;
console.log(convertToPositive(negativeNumber));
Output
5
Note: For simply converting a number to its positive equivalent, Math.abs() is the most direct and recommended approach.