Your Complete Guide To Math Object in Javascript
Tags: javascript, webdev, tutorial, typescript
Published: 2021-07-19 02:04:50

Hi everybody, I'm [Aya Bouchiha](https://t.me/AyaBouchiha), and this is your complete guide to **Math object** in Javascript!

# Math Object

Firstly, we need to know that Math is a built-in object, It works with *Number* type and not with *BigInt*. In addition, It is not a constructor. All its properties and its methods are static.

## Math.floor()

**Math.floor**(*number*) : rounds a number down and returns an integer value.

```javascript
/* rounds a number down  */
console.log(Math.floor(2.4)) // 2
console.log(Math.floor(1.999999)) // 1
console.log(Math.floor(10.5)) // 10
```

## Math.ceil()

**Math.ceil**(*number*) : rounds a number up to the next largest integer.

```javascript
/* rounds a number up to the next largest integer  */
console.log(Math.ceil(0.000000000000000001)) // 1
console.log(Math.ceil(0) )// 0
console.log(Math.ceil(10.5)) // 11
```
## Math.random()

**Math.random()** : returns a random number where `0 <= Math.random() < 1`

```javascript
/* get a random number n where 0 <= n < 1 */
console.log(Math.random()) //  0.3594237846698176
 
// Returns a random integer from 0 to 50:
console.log(Math.floor(Math.random() * 50)) // 43
```

## Math.round()

**Math.round**(*number*) : rounds to the nearest integer

```javascript
/* rounds to the nearest integer  */
console.log(Math.round(0.2)) // 0
console.log(Math.round(10.5)) // 11
console.log(Math.round(1.9) )// 2
```

## Math.trunc()

**Math.trunc**(*number*) : returns the integer part of a number by removing any fractional digits.

```javascript
/*  get the integer part of a number */
console.log(Math.trunc(1.000000001)) // 1
console.log(Math.trunc(10.5)) // 10
console.log(Math.trunc(4.999999)) // 4
```

## Math.sign()

**Math.sign**(*number*) :indicate the sign of a numeber. If the num is:
+ *negative* : returns -1
+ *0* : returns 0
+ *positive* : returns 1

```javascript
/*  get the sign of a number  */
console.log(Math.sign(-4.5)) // -1
console.log(Math.sign(0)) // 0
console.log(Math.sign(10)) // 1
```

## Math.pow()

**Math.pow**(*base, exponent*) : returns base<sup>exponent</sup>.

```javascript
/*  get the value of a num1 to the power of a num2  */
console.log(Math.pow(2,3)) // 8
console.log(Math.pow(1,10)) // 1
console.log(Math.pow(10,3)) // 1000
```

## Math.sqrt()

**Math.sqrt**(*num*) : returns the square root of a number.

```javascript
/*  get  the square root of a number. */
console.log(Math.sqrt(16)) // 4
console.log(Math.sqrt(100)) // 10
console.log(Math.sqrt(25)) // 5
```

## Math.cbrt()

**Math.cbrt**(*num*) : returns the cubic root of a number.

```javascript
/*  get  the cubic root of a number. */
console.log(Math.cbrt(8)) // 2
console.log(Math.cbrt(27)) // 3
console.log(Math.cbrt(64)) // 4
```

## Math.log2()

**Math.log2**(*num*) : returns the base 2 logarithm of a number,

```javascript
/*  get  the base 2 logarithm of a number */
console.log(Math.log2(2)) // 1
console.log(Math.log2(8)) // 3
console.log(Math.log2(16)) // 4
```

## Math.min()

**Math.min**(*n1, n2, n3, ..*) : returns The **smallest** number of the given numbers. If one of the giving arguments is not a number  It returns **NaN**, and **_Infinity_** if no arguments are passed.

```javascript
/*  get  the smallest of the given numbers. */
console.log(Math.min(2, 4, 6, 8)) // 2
console.log(Math.min(...[20, 10, 100, 70])) // 10
console.log(Math.min(7, "Aya", "Bouchiha", 3)) // NaN
console.log(Math.min()) // Infinity
```

## Math.max()

**Math.max**(*n1, n2, n3, ..*) : returns The **biggest** number of the given numbers. If one of the giving arguments is not a number  it returns **NaN**, and **_-Infinity_** if no arguments are passed.

```javascript
/*  get  the biggest num of the given numbers. */
console.log(Math.max(2, 4, 6, 8)) // 8
console.log(Math.max(...[20, 10, 100, 70])) // 100
console.log(Math.max(7, "Aya", "Bouchiha", 3)) // NaN
console.log(Math.max()) // -Infinity
```

## Math.abs()

**Math.abs**(*num*) : returns the absolute value of the giving number. This method can accept also numeric strings like `'-1'` .
+ It returns **NaN** if the giving argument is not a number or is not a numeric-string, or It is undefined or empty . 
+ It returns **0** if the argument is `null` , `[]` , `''` or `""`

```javascript
/*  get  the absolute value of the given number */
console.log(Math.abs(-2)) // 2 
console.log(Math.abs(0))  // 0
console.log(Math.abs(4.5)) // 4.5
console.log(Math.abs('')) // 0
console.log(Math.abs(null)) // 0
console.log(Math.abs([])) // 0
console.log(Math.abs('Aya Bouchiha')) // NaN
console.log(Math.abs([1, 2, 3, 4])) // NaN
console.log(Math.abs({})) // NaN 
console.log(Math.abs()) //  NaN
console.log(Math.abs(undefined)) // NaN
```


## Math.tan()

**Math.tan**(*angleInRadians*) :  returns the tangent of a giving angle(*radians*).

```javascript
/*  get  the tangent an angle(radians) */
console.log(Math.tan(1)) // 1.5574077246549023
console.log(Math.tan(2)) // -2.185039863261519
console.log(Math.tan()) // NaN
console.log(Math.tan('')) // 0
```
## Math.sin()

**Math.sin**(*angleInRadians*) :  returns the sine of a giving angle
(*radians*), in addition, it is between -1 & 1.

```javascript
/*  get  the sine of an angle(radians) */
console.log(Math.sin(2)) // 0.9092974268256817
console.log(Math.sin(1)) // 0.8414709848078965
console.log(Math.sin()) // NaN
console.log(Math.sin('')) // 0
```

## Math.cos()

**Math.cos**(*angleInRadians*) :  returns the cosine of a giving angle
(*radians*), in addition, it is between -1 & 1.

```javascript
/*  get  the cosine of an angle(radians) */
console.log(Math.cos(2)) // -0.4161468365471424
console.log(Math.cos(1)) // 0.5403023058681398
console.log(Math.cos()) // NaN
console.log(Math.cos('')) // 1
```

## Math.exp()

**Math.exp**(*number*) :  returns e<sup>x</sup> 

## Math.Pi
**Math.Pi** : is a static property of Math that returns the value of PI (*approximately 3.14*)

```javascript
/* PI */
const Pi = Math.PI
const CalculateAreaOfACircle = radius => Math.round(Pi* (radius ** 2));
console.log(Pi) // 3.141592653589793
console.log(CalculateAreaOfACircle(4)) //  50
```

## Math.E

**Math.E** : is a static property of Math that returns the value of  Euler's number *(approximately 2.718*)

```javascript
/* Euler's numbe */
const e = Math.E
console.log(e) // 2.718281828459045
```

## Math.LN2

**Math.E** : is a static property of Math that returns the natural logarithm of 2 *(approximately  0.693*)

```javascript
/* natural logarithm of 2 */
console.log(Math.LN2) // 0.6931471805599453
```

## Final Code

[Final Code on github](https://github.com/Aya-Bouchiha/Math-Object-In-Javascript/blob/main/MathObjectGuide.js)


[more details(mdn)...](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math)

Have A  Nice Day!

+ email: developer.aya.b@gmail.com
+ [telegram](https://t.me/AyaBouchiha): Aya Bouchiha