Published 2022. 11. 17. 09:13

The Math Object

the Math object has no constructor.

The Math object is static.

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 
"<p><b>Math.E:</b> " + Math.E + "</p>" +
"<p><b>Math.PI:</b> " + Math.PI + "</p>" +
"<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" +
"<p><b>Math.SQRT1_2:</b> " + Math.SQRT1_2 + "</p>" +
"<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" +
"<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" +
"<p><b>Math.LOG2E:</b> " + Math.LOG2E + "</p>" +
"<p><b>Math.Log10E:</b> " + Math.LOG10E + "</p>";
</script>
Math.E: 2.718281828459045
Math.PI: 3.141592653589793
Math.SQRT2: 1.4142135623730951
Math.SQRT1_2: 0.7071067811865476
Math.LN2: 0.6931471805599453
Math.LN10: 2.302585092994046
Math.LOG2E: 1.4426950408889634
Math.Log10E: 0.4342944819032518

 

 

Number to Integer

Math.round(x) Returns x rounded to its nearest integer
Math.ceil(x) Returns x rounded up to its nearest integer
Math.floor(x) Returns x rounded down to its nearest integer
Math.trunc(x) Returns the integer part of x (new in ES6)

 

 

 

 

Math.round()

returns the nearest integer:

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.round(4.4);
</script>
4

 

 

Math.ceil()

returns the value of x rounded up to its nearest integer.

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.ceil(4.4);
</script>
5

 

 

 

 

Math.floor()

returns the value of x rounded down to its nearest integer.

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.floor(4.7);
</script>
4

 

 

 

Math.trunc()

returns the integer part of x.

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.trunc(4.7);
</script>
4

 

 

 

 

Math.sign()

returns if x is negative, null or positive.

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.sign(4);
</script>
1
<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.sign(-4);
</script>
-1

 

 

 

Math.pow()

returns the value of x to the power of y.

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.pow(8,2);
</script>
64

 

 

 

Math.sqrt()

returns the square root of x.

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.sqrt(64);
</script>
8

 

 

 

Math.abs()

returns the absolute (positive) value of x.

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.abs(-4.7);
</script>
4.7

 

 

 

 

Math.min() and Math.max()

Math.min(0, 150, 30, 20, -8, -200);
Math.max(0, 150, 30, 20, -8, -200);

 

 

 

 

 

Math.random()

returns a random number between 0 (inclusive), and 1 (exclusive).

 

 

 

Math.log() Method

eturns the natural logarithm of x.

 

Math.log2() Method

Math.log10() Method

 

 

 

A Proper Random Function

<p>Every time you click the button, getRndInteger(min, max) returns a random number between 0 
and 9 (both included):</p>

<button onclick="document.getElementById('demo').innerHTML = getRndInteger(0,10)">Click Me</button>

<p id="demo"></p>

<script>
function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}
</script>
Every time you click the button, getRndInteger(min, max) returns a random number between 0 and 9 (both included):
Click Me

0

 

 

<p>Every time you click the button, getRndInteger(min, max) returns a random number between 1 and 10 (both included):</p>

<button onclick="document.getElementById('demo').innerHTML = getRndInteger(1,10)">Click Me</button>

<p id="demo"></p>

<script>
function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}
</script>
Every time you click the button, getRndInteger(min, max) returns a random number between 1 and 10 (both included):
Click Me

9

 

'JavaScript' 카테고리의 다른 글

JavaScript - Comparison  (0) 2022.11.17
JavaScript - Booleans  (0) 2022.11.17
JavaScript - Set Date Methods  (0) 2022.11.17
JavaScript - Date Formats  (0) 2022.11.16
JavaScript - Array Iteration  (0) 2022.11.15
복사했습니다!