|
1 | | -// INFO: Math is JavaScript |
| 1 | +// INFO: Math in JavaScript |
| 2 | + |
| 3 | +// NOTE: Math Object Operators and Methods |
2 | 4 |
|
3 | | -// NOTE: Mathematical Operators |
4 | 5 | /* |
5 | | -1. Addition Operator: + |
6 | | -2. Subtraction Operator: - |
7 | | -3. Multiplication Operator: * |
8 | | -4. Division Operator: / |
9 | | -5. Remainder Operator: % |
10 | | -6. Exponentiation Operator: ** |
| 6 | +Math Operators: |
| 7 | +1. Exponentiation: ** |
| 8 | + (Alternative: Math.pow) |
| 9 | +
|
| 10 | +Math Object Properties: |
| 11 | +- Math.PI → Returns the value of π |
| 12 | +
|
| 13 | +Math Object Methods: |
| 14 | +- Math.sqrt(x) → Square root of x |
| 15 | +- Math.floor(x) → Rounds down to nearest integer |
| 16 | +- Math.ceil(x) → Rounds up to nearest integer |
| 17 | +- Math.round(x) → Rounds to nearest integer |
| 18 | +- Math.random() → Returns a random number between 0 and 1 |
11 | 19 | */ |
12 | 20 |
|
13 | | -console.log(1 + 1); |
14 | | -console.log(2 - 2); |
15 | | -console.log(2 * 6); |
16 | | -console.log(2 / 6); |
17 | | -console.log(2 % 4); |
18 | | - |
19 | | -// this two works same but the recommendation is the first one |
20 | | -console.log(2 ** 4); |
21 | | -console.log(Math.pow(2, 4)); |
22 | | - |
23 | | -// Properties of Math Object |
24 | | -console.log(Math.PI); |
25 | | - |
26 | | -// Methods of Math Object |
27 | | -console.log(Math.sqrt(16)); // 4 |
28 | | -console.log(Math.floor(2.99)); // 2 |
29 | | -console.log(Math.floor(-2.5)); // -3 |
30 | | -console.log(Math.ceil(2.1)); // 3 |
31 | | -console.log(Math.ceil(-2.5)); // -2 |
32 | | -console.log(Math.round(2.5)); // 3 |
33 | | -console.log(Math.round(2.4)) // 2 |
34 | | -console.log((Math.random())); // any random value |
35 | | -console.log(Math.round(Math.random() * 20)); // gives the random value 1 to 20 |
| 21 | +// Exponentiation (Preferred way) |
| 22 | +console.log(2 ** 4); // Output: 16 |
| 23 | + |
| 24 | +// Exponentiation (Alternative) |
| 25 | +console.log(Math.pow(2, 4)); // Output: 16 |
| 26 | + |
| 27 | +// Math Property |
| 28 | +console.log(Math.PI); // Output: 3.141592653589793 |
| 29 | + |
| 30 | +// Math Methods |
| 31 | +console.log(Math.sqrt(16)); // Output: 4 |
| 32 | + |
| 33 | +console.log(Math.floor(2.99)); // Output: 2 |
| 34 | +console.log(Math.floor(-2.5)); // Output: -3 |
| 35 | + |
| 36 | +console.log(Math.ceil(2.1)); // Output: 3 |
| 37 | +console.log(Math.ceil(-2.5)); // Output: -2 |
| 38 | + |
| 39 | +console.log(Math.round(2.5)); // Output: 3 |
| 40 | +console.log(Math.round(2.4)); // Output: 2 |
| 41 | + |
| 42 | +console.log(Math.random()); // Output: Random value between 0 and 1 |
| 43 | +console.log(Math.round(Math.random() * 20)); // Output: Random integer from 0 to 20 |
0 commit comments