Categories
JavaScript Answers

How to convert positive number to negative number in JavaScript?

Spread the love

Sometimes, we want to convert positive number to negative number in JavaScript.

In this article, we’ll look at how to convert positive number to negative number in JavaScript.

How to convert positive number to negative number in JavaScript?

To convert positive number to negative number in JavaScript, we can use the - operator.

For instance, we write

const n = -Math.abs(num);

to call Math.abs with num to get the absolute value of num.

Then we use the unary - operator to convert the absolute value to a negative number.

Conclusion

To convert positive number to negative number in JavaScript, we can use the - operator.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *