take the absolute value of a number in typescript

In TypeScript, you can take the absolute value of a number by using the built-in Math.abs() function. This function takes a number as an argument and returns its absolute value. Here's an example:

index.ts
let num: number = -10;
let absNum: number = Math.abs(num); // absNum will be 10
80 chars
3 lines

In this example, we declare a variable num and set it to the value of -10. We then call Math.abs(num) to get its absolute value and assign it to the variable absNum.

Note that the Math.abs() function can also be used with other numeric types, such as BigInt or Decimal.

gistlibby LogSnag