You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Math object in JavaScript is a collection of number-related utility functions and constants. It provides a namespace to group these functionalities and avoid polluting the global namespace.
3
+
## 📜 Introduction
4
+
The **Math** object in JavaScript is packed with useful functions and constants for performing numerical and trigonometric calculations. It helps in **organizing number-related utilities**and **avoids polluting the global scope**.
5
5
6
-
## Why Use the Math Object?
7
-
Using the Math object helps prevent conflicts in naming by keeping utility functions and constants within a single object. This avoids overwriting existing bindings in your code.
6
+
## 🤔 Why Use the Math Object?
7
+
-**Namespace Organization**: Groups all math-related utilities within a single object, preventing conflicts with other variables.
8
+
-**Utility Functions**: Provides essential mathematical operations without manually implementing them.
8
9
9
-
## Common Math Functions
10
+
## 🧮 Common Math Functions
10
11
11
-
### Math.max and Math.min
12
-
These functions return the maximum and minimum values from a list of numbers.
12
+
### 📈 Math.max and Math.min
13
+
Find the maximum and minimum values from a list of numbers.
13
14
14
-
#### Example
15
15
```javascript
16
16
console.log(Math.max(3, 7, 2, 8)); // → 8
17
17
console.log(Math.min(3, 7, 2, 8)); // → 2
18
18
```
19
19
20
-
### Math.sqrt
21
-
The `Math.sqrt` function returns the square root of a number.
20
+
### 📏 Math.sqrt
21
+
Calculates the square root of a given number.
22
22
23
-
#### Example
24
23
```javascript
25
24
console.log(Math.sqrt(16)); // → 4
26
25
```
27
26
28
-
## Trigonometric Functions
29
-
The Math object includes trigonometric functions such as `Math.cos` (cosine), `Math.sin` (sine), and `Math.tan` (tangent), as well as their inverse functions `Math.acos`, `Math.asin`, and `Math.atan`.
27
+
## 📐 Trigonometric Functions
28
+
29
+
Math provides various trigonometric functions such as `Math.cos` (cosine), `Math.sin` (sine), `Math.tan` (tangent), and their inverses `Math.acos`, `Math.asin`, `Math.atan`.
30
+
31
+
### Example: Generating a Random Point on a Circle
32
+
Using `Math.cos` and `Math.sin` with `Math.PI` (π), this function returns a random point on a circle of a given radius.
30
33
31
-
### Example: Using Trigonometric Functions
32
34
```javascript
33
35
functionrandomPointOnCircle(radius) {
34
36
let angle =Math.random() *2*Math.PI;
@@ -39,65 +41,63 @@ function randomPointOnCircle(radius) {
39
41
}
40
42
41
43
console.log(randomPointOnCircle(2));
42
-
//→ {x: 0.3667, y: 1.966}
44
+
//Example Output: {x: 0.3667, y: 1.966}
43
45
```
44
-
In this example, `Math.PI` is used to get the value of π (pi), and trigonometric functions generate a random point on a circle with a given radius.
45
46
46
-
## Math.random
47
-
The `Math.random` function generates a pseudorandom number between 0 (inclusive) and 1 (exclusive).
47
+
### Explanation
48
+
-`Math.PI`: Provides π for precise circle calculations.
49
+
-**Trigonometric Functions**: Generate coordinates for a random angle on the circle.
50
+
51
+
## 🎲 Math.random
52
+
53
+
The `Math.random` function generates a **pseudorandom number** between 0 (inclusive) and 1 (exclusive).
console.log(Math.random()); // Example Output: 0.36993729369714856
54
57
```
55
58
56
59
### Generating Whole Random Numbers
57
-
To generate a whole random number, multiply the result of `Math.random` by a desired range and use `Math.floor` to round down to the nearest whole number.
60
+
To get whole numbers, multiply the result of `Math.random` by a range and use `Math.floor` to round down.
This expression produces a random integer from 0 to 9.
64
65
65
-
## Other Useful Math Functions
66
+
### Explanation
67
+
-**Multiplying by Range**: Expands the random number’s range.
68
+
-**Math.floor**: Rounds down to generate an integer within the specified range.
69
+
70
+
## 🔧 Other Useful Math Functions
66
71
67
-
### Math.floor
68
-
Rounds down to the nearest whole number.
72
+
### 🔻 Math.floor
73
+
Rounds **down** to the nearest integer.
69
74
70
-
#### Example
71
75
```javascript
72
76
console.log(Math.floor(3.9)); // → 3
73
77
```
74
78
75
-
### Math.ceil
76
-
Rounds up to the nearest whole number.
79
+
### 🔺 Math.ceil
80
+
Rounds **up** to the nearest integer.
77
81
78
-
#### Example
79
82
```javascript
80
83
console.log(Math.ceil(3.1)); // → 4
81
84
```
82
85
83
-
### Math.round
84
-
Rounds to the nearest whole number.
86
+
### ⚖️ Math.round
87
+
Rounds to the **nearest integer**.
85
88
86
-
#### Example
87
89
```javascript
88
90
console.log(Math.round(3.5)); // → 4
89
91
console.log(Math.round(3.4)); // → 3
90
92
```
91
93
92
-
### Math.abs
93
-
Returns the absolute value of a number (negates negative values).
94
+
### ➖ Math.abs
95
+
Returns the **absolute value** of a number, making all values non-negative.
94
96
95
-
#### Example
96
97
```javascript
97
98
console.log(Math.abs(-5)); // → 5
98
99
console.log(Math.abs(5)); // → 5
99
100
```
100
101
101
-
## Conclusion
102
-
The Math object in JavaScript provides a robust set of utility functions for mathematical operations. By using the Math object, you can perform complex calculations, generate random numbers, and utilize trigonometric functions without polluting the global namespace.
103
-
102
+
## ✅ Conclusion
103
+
The **Math object** in JavaScript is essential for **performing calculations** ranging from basic arithmetic to advanced trigonometry. By utilizing these methods, developers can efficiently manage numerical data without cluttering the global namespace.
0 commit comments