What are number methods and how do they impact performance?
Tags: webdev, programming, tutorial, javascript
Published: 2023-08-19 14:42:43

Number methods are built-in methods that intend to make working with numbers easier and more effective by handling operations, conversions and checks.

Overview: 
1. [Number methods](#methods)
2. [Number properties](#properties)
3. [How number methods affect performance](#performance).

## <a id="methods">Number Methods</a>
Number methods, allow you to manipulate numbers in various ways without the need to write custom code. 

| Method                   | Description                                     |
|--------------------------|-------------------------------------------------|
| `toFixed(digits)`        | Formats a number as a string with fixed decimals |
| `toString()`             | Converts a number to a string                   |
| `parseInt(str)`          | Parses a string to an integer                   |
| `parseFloat(str)`        | Parses a string to a decimal number      |
| `isNaN(value)`           | Checks if a value is NaN (Not-a-Number)         |
| `isFinite(value)`        | Checks if a value is a finite number            |
| `toExponential(digits)`  | Formats a number in exponential notation       |
| `toPrecision(precision)` | Formats a number with a specified precision    |

- toFixed() 
```javascript
let num = 5.324578;
let formattedNum = num.toFixed(2); // "5.32"
```
- toString() 
```javascript
let num = 15;
let str = num.toString(); // "15" 
```
- parseInt(str)
```javascript
let str = "543";
let newInt = parseInt(str); // 543
```
- parseFloat()
```javascript
let floatStr = "1.23";
let parsedFloat = parseFloat(floatStr); // 1.23
```
- isNaN()
 The `isNaN()` method checks if the value passed to it can be converted to a number or not. It returns a boolean value `true` or `false`. if the value can be converted to a number it returns `false`, otherwise it returns `true`.

```javascript
isNaN("hello");  // true 
isNaN(26);       // false
```
- isFinite()

```javascript
let finiteValue = 42;
let infiniteValue = Infinity;
const finiteCheck = isFinite(finiteValue);     // true
const infiniteCheck = isFinite(infiniteValue); // false
```
In this example we've used the `Infinity` keyword which is a special numeric value that demonstrates positive infinity. That is why the `infinityCheck` returned `false`

- toExponential() 

Exponential notation are a form of notation where very large or very small numbers can be expressed in a smaller and practical form. For example
```text
2000 = 2 x 10^3
0.0012 = 1.2 x 10^-3
```
The `toExponential()` method formats number into exponential notation with the specified number of decimal as argument.
```javascript
let num = 54321.9876;
let expNum = num.toExponential(2); // 5.43e+4
```

- toPrecision() 
This method formats numbers with a predetermined total number of significant digits. 

```javascript
let num = 226.456;
let preciseNum = num.toPrecision(5); //226.46
```
In this example we've passed 5 as argument which tells the method to return a number of 5 digits.

## <a id="properties">Number properties</a>
Number properties give important information and constants related to numeric values. they provide explanations for the properties and limitations of numbers.

| Property           | Description                                          |
|--------------------|------------------------------------------------------|
| `Number.MAX_VALUE` | The largest representable number in JavaScript       |
| `Number.MIN_VALUE` | The smallest positive representable number in JavaScript (close to zero) |
| `Number.POSITIVE_INFINITY` | Represents positive infinity (e.g., 1/0)    |
| `Number.NEGATIVE_INFINITY` | Represents negative infinity (e.g., -1/0)    |
| `Number.NaN`      | Represents "Not-a-Number"                            |
| `Number.EPSILON`  | The smallest difference between two representable numbers |
| `Number.MAX_SAFE_INTEGER` | The largest integer that can be safely represented without loss of precision |
| `Number.MIN_SAFE_INTEGER` | The smallest integer that can be safely represented without loss of precision |

- MAX_VALUE is `1.7976931348623157e+308`
- MIN_VALUE is `5e-324`
- POSITIVE_INFINITY is `Infinity`
- NEGATIVE_INFINITY is `-Infinity`
- NaN is `NaN` (not a number)
- EPSILON is  `2.220446049250313e-16`
- MAX_SAFE_INTEGER is `9007199254740991`
- MIN_SAFE_INTEGER is `-9007199254740991`

## <a id="performance">How number methods affect performance</a>

Performance in JavaScript is significantly impacted by the use of number methods, particularly when performing calculations, data processing, and algorithmic operations. These techniques offer a number of advantages, such as quick operations that produce code that is cleaner and more concise, the maintenance of accuracy and precision with techniques like `toFixed()`. To benefit from these advantages, become familiar with number methods, but test your code first to ensure that it satisfies your performance needs.

Along this series we'll see more methods such as the `Math` Object methods that will allow us to perform many mathematical operations.