|
| 1 | +// INFO: Math Object in JavaScript |
| 2 | + |
| 3 | +/* |
| 4 | +The Math object in JavaScript provides properties and methods for mathematical constants and functions. |
| 5 | +
|
| 6 | +--- |
| 7 | +
|
| 8 | +INFO: Math Operators: |
| 9 | +
|
| 10 | +1. Exponentiation: ** |
| 11 | + - Raises a number to the power of another. |
| 12 | + - Alternative: Math.pow(base, exponent) |
| 13 | +
|
| 14 | +--- |
| 15 | +
|
| 16 | +INFO: Math Object Properties: |
| 17 | +
|
| 18 | +1. Math.PI |
| 19 | +The ratio of the circumference of a circle to its diameter (≈ 3.14159) |
| 20 | +
|
| 21 | +2. MATH.E |
| 22 | +Euler's number (≈ 2.718), the base of natural logarithms. |
| 23 | +
|
| 24 | +3. MATH.LN2 |
| 25 | +Natural logarithm of 2. |
| 26 | +
|
| 27 | +4. MATH.LN10 |
| 28 | +Natural logarithm of 10. |
| 29 | +
|
| 30 | +5. MATH.LOG2E |
| 31 | +Base-2 logatithm of E. |
| 32 | +
|
| 33 | +6. MATH.SQRT1_2 |
| 34 | +Square root of 1/2 (≈ 0.707) |
| 35 | +
|
| 36 | +7. MATH.SQRT2 |
| 37 | +Square root of 2 (≈ 1.414) |
| 38 | +
|
| 39 | +--- |
| 40 | +
|
| 41 | +INFO: Math Object Methods: |
| 42 | +
|
| 43 | +1. Math.round() |
| 44 | +Rounds a number to the nereast integer. |
| 45 | +
|
| 46 | +2. Math.floor() |
| 47 | +Always rounds down to the nereast integer. |
| 48 | +
|
| 49 | +3. Math.ceil() |
| 50 | +Always rounds up to the nereast integer. |
| 51 | +
|
| 52 | +4. Math.random() |
| 53 | +Returns a random decimal number between 0 (inclusive) & 1 (exclusive). |
| 54 | +
|
| 55 | +5. Math.max() |
| 56 | +Returns the largest number from a set of numbers. |
| 57 | +
|
| 58 | +6. Math.min() |
| 59 | +Returns the smallest number from a set of numbers. |
| 60 | +
|
| 61 | +7. Math.pow() |
| 62 | +Returns base raised to the power of exponent (base ^ exponent). |
| 63 | +
|
| 64 | +8. Math.sqrt() |
| 65 | +Returns the square root of a number. |
| 66 | +
|
| 67 | +9. Math.abs() |
| 68 | +Returns the absolute (positive) value of a number. |
| 69 | +
|
| 70 | +10. Math.trunc() |
| 71 | +Removes the decimal part and returns only the integer. |
| 72 | +
|
| 73 | +*/ |
| 74 | + |
| 75 | +// INFO: MATH Object Operators |
| 76 | + |
| 77 | +// Exponentiation |
| 78 | +console.log(2 ** 4); |
| 79 | +console.log(Math.pow(2, 4)); |
| 80 | + |
| 81 | +// INFO: MATH Object Properties |
| 82 | + |
| 83 | +// Math.PI |
| 84 | +console.log(Math.PI); |
| 85 | + |
| 86 | +// MATH.E |
| 87 | +console.log(MATH.E); |
| 88 | + |
| 89 | +// MATH.LN2 |
| 90 | +console.log(MATH.LN2); |
| 91 | + |
| 92 | +// MATH.LN10 |
| 93 | +console.log(MATH.LN10); |
| 94 | + |
| 95 | +// MATH.LOG2E |
| 96 | +console.log(MATH.LOG2E); |
| 97 | + |
| 98 | +// MATH.SQRT1_2 |
| 99 | +console.log(MATH.SQRT1_2); |
| 100 | + |
| 101 | +// MATH.SQRT2 |
| 102 | +console.log(MATH.SQRT2); |
| 103 | + |
| 104 | +// INFO: MATH Object Methods |
| 105 | + |
| 106 | +// MATH.round() |
| 107 | +console.log(4.5); // 5 |
| 108 | +console.log(4.4); // 4 |
| 109 | + |
| 110 | +// MATH.floor() |
| 111 | +console.log(Math.floor(4.9)); // 4 |
| 112 | +console.log(Math.floor(-4.1)); // -5 |
| 113 | + |
| 114 | +// MATH.ceil() |
| 115 | +console.log(Math.ceil(4.1)); // 5 |
| 116 | +console.log(Math.ceil(-4.9)); // -4 |
| 117 | + |
| 118 | +// Math.random() |
| 119 | +console.log(Math.random()); // Random float between 0 and 1 |
| 120 | + |
| 121 | +// Math.max() |
| 122 | +console.log(Math.max(3, 10, 2)); // 10 |
| 123 | + |
| 124 | +// Math.min() |
| 125 | +console.log(Math.min(3, 7, 1, 9)); // 1 |
| 126 | + |
| 127 | +// MATH.pow() |
| 128 | +console.log(MATH.pow(2, 3)); // 8 (2 ^ 3) |
| 129 | + |
| 130 | +// MATH.sqrt() |
| 131 | +console.log(MATH.sqrt(16)); // 4 |
| 132 | + |
| 133 | +// MATH.abs() |
| 134 | +console.log(MATH.abs(-5)); // 5 |
| 135 | + |
| 136 | +// MATH.trunc() |
| 137 | +console.log(MATH.trunc(4.9)); // 4 |
| 138 | +console.log(MATH.trunc(-4.9)); // -4 |
| 139 | + |
0 commit comments