JavaScript Number Methods
JavaScript provides several built-in methods for performing operations on numbers. These methods are available on number objects and can be used to manipulate numbers in various ways.
1. toFixed()
Returns a string representing a number with a specified number of decimal places.
Example:
const num = 3.14159265359;
console.log(num.toFixed(2)); // Output: "3.14"
2. toPrecision()
Returns a string representing a number in fixed-point or exponential notation, depending on the specified precision.
Example:
const num = 12345;
console.log(num.toPrecision(3)); // Output: "1.23e+4"
3. toString()
Returns a string representing the specified number.
Example:
const num = 42;
console.log(num.toString()); // Output: "42"
4. toExponential()
Returns a string representing a number in exponential notation with a specified number of digits.
Example:
const num = 12345;
console.log(num.toExponential(2)); // Output: "1.23e+4"
These are just a few examples of the number methods available in JavaScript. Each method serves a specific purpose and can be used to manipulate numbers according to your needs.