Convert a negative number to positive in JavaScript
Last Updated :
29 Dec, 2023
In this article, we will see how we can convert a negative number to a positive number in JavaScript by the methods described below.
Below are the methods to convert a negative number to a positive in JavaScript:
Method 1: Multiplying by -1
This is a general method in which we will first check whether the number is already positive or negative, if the number is negative then we will multiply the number by -1 to make it positive.
Syntax:
if (a < 0) {
a = a * -1;
}
Example: Below is the implementation of the above approach
javascript
// Javascript script to convert negative number
// to positive number
// Function to convert
// given number to
// positive number
function convert_positive(a) {
// Check the number is negative
if (a < 0) {
// Multiply number with -1
// to make it positive
a = a * -1;
}
// Return the positive number
return a;
}
//Driver code
let n = -10;
let m = 5;
// Call function
n = convert_positive(n);
// Print result
console.log(n);
// Call function
m = convert_positive(m);
// Print result
console.log(m);
In this method, we will use Math.abs() function to convert negative numbers to positive numbers.
Syntax:
Math.abs(value)
Example: Below is the implementation of the above approach:
javascript
// Javascript script to convert negative number
// to positive number
//Driver code
let n = -30;
let m = 15;
// Using Math.abs() function
n = Math.abs(n);
// Print result
console.log(n);
// Using Math.abs() function
m = Math.abs(m);
// Print result
console.log(m);
Method 3: adding a minus sign
In this method, we will check whether the number is positive or negative, if the number is negative then we add a minus sign at the beginning of the number else return the same.
Syntax:
a < 0 ? -(a) : a
Example: This example shows the above-explained approach.
JavaScript
// Javascript script
// to convert negative number
// to positive number
// Function to convert
// given number to
// positive number
function convert_positive(a) {
return a < 0 ? -(a) : a;
}
//Driver code
let n = -10;
let m = 5;
// Call function
n = convert_positive(n);
// Print result
console.log(n);
// Call function
m = convert_positive(m);
// Print result
console.log(m);
Method 4: Flipping the bit
In this method, we will use a bit-wise not operator which flips all the bits of the number. Since the negative number is stored in a most significant bit it also flips which converts it to a positive number.
Syntax:
a < 0 ? ( ~a + 1 ) : a
Example: In this example, we Flipping the bit to Convert a negative number to positive in JavaScript.
JavaScript
// Javascript script
// to convert negative number
// to positive number
// Function to convert
// given number to
// positive number
function convert_positive(a) {
return a < 0 ? (~a + 1) : a;
}
//Driver code
let n = -10;
let m = 5;
// Call function
n = convert_positive(n);
// Print result
console.log(n);
// Call function
m = convert_positive(m);
// Print result
console.log(m);
To convert a negative number to its positive equivalent in JavaScript using Math.sqrt()
, you can use the Math.sqrt()
function along with the Math.pow()
function.
Example: In this example, we are using Math.sqrt().
JavaScript
function convertToPositive(number) {
// Using Math.sqrt() and Math.pow() to convert a negative number to positive
return Math.sqrt(Math.pow(number, 2));
}
// Example usage
const negativeNumber = -5;
const positiveResult = convertToPositive(negativeNumber);
console.log(positiveResult); // Output: 5
Similar Reads
JavaScript Program to Print Multiplication Table of a Number We are given a number n as input, we need to print its table. Below are the different approaches to print multiplication table in JavaScriptJavaScript Program to print multiplication table of a number1. Using a for LoopThis is the most common way to print the multiplication table. A for loop repeats
3 min read
What is the use of Math object in JavaScript ? The Math object in JavaScript provides a set of methods and properties for mathematical constants and functions. It is a built-in object that allows for performing mathematical operations and accessing mathematical constants without creating an instance.What is the Math Object?The Math object is an
4 min read
How to get median of an array of numbers in JavaScript ? In this article, we will see how to find the median of an array using JavaScript. A median is a middle value in a given set of numbers or data. Examples: Input arr1 : [1 4 7 9]Output : 5.5Explanation : The median of the two sorted array is the middle elements which is 5 if the arrayLength =arr1.leng
3 min read
How to calculate greatest common divisor of two or more numbers/arrays in JavaScript ? In this article, we are given two or more numbers/array of numbers and the task is to find the GCD of the given numbers/array elements in JavaScript. Examples: Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {2, 4, 6, 8} Output : 2 The GCD of three or more numbers equals the product of the prim
2 min read
How to test a value x against predicate function and returns fn(x) or x in JavaScript ? In this article, we are given a value x, and the task is to check the value against the predicate function. If the value satisfies the condition or predicate return fn(x) else return x. Predicate functions are the function that takes one item as input and returns true or false based on the whether i
3 min read
How to Flatten a Given Array up to Specified Depth in JavaScript? Flatten an array in JavaScript is a common task when dealing with nested arrays. The concept of "flatten" means to convert a multi-dimensional array into a single-dimensional array. When you need to control the depth of flattening, JavaScript provides methods to handle this efficiently. This process
2 min read
How to get the standard deviation of an array of numbers using JavaScript ? Given an array and the task is to calculate the standard deviation using JavaScript. Example: Input: [1, 2, 3, 4, 5]Output: 1.4142135623730951Input: [23, 4, 6, 457, 65, 7, 45, 8]Output: 145.13565852332775Please refer to Mean, Variance, and Standard Deviation for details. Mean is average of element.
3 min read
How to evaluate binomial coefficient of two integers n and k in JavaScript ? The following are the common definitions of Binomial Coefficients. A binomial coefficient C(n, k) can be defined as the coefficient of x^k in the expansion of (1 + x)^n. Binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects
2 min read
How to check two numbers are approximately equal in JavaScript ? In this article, we are given two numbers and the task is to check whether the given numbers are approximately equal to each other or not. If both numbers are approximately the same then print true otherwise print false. Example: Input: num1 = 10.3 num2 = 10 Output: true Approach: To check whether t
2 min read
How to create slider to map a range of values in JavaScript ? The task is to map a range of values to another range of values like map (0-100) to (100-10000000) in JavaScript. There are two approaches that are discussed below.Approach 1: Use the logarithmic scale to map the range. In this example, first, the log value of the range is calculated and then the sc
2 min read