Make number positive in JavaScript

1. Use Math.abs().

Math.abs(number);

2. Multiply the number by -1 if it’s negative.

if(number < 0) {
  number = number * -1;
}

Example:

Convert a negative to positive number in JavaScript.

<script>
  var myNumber = -8;
  myNumber = Math.abs(myNumber);

  console.log(myNumber);
</script>

Output: 8

Example 2:

<script>
  var myNumber = -100;

  if(myNumber < 0) {
    myNumber = myNumber * -1;
  }
  
  console.log(myNumber);
</script>

Output: 100