DONATE  
MENU
PHP
JSON
HTML
JAVASCRIPT
MYSQL
AJAX
CSS

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Math Object » Math Methods
Share Tweet WhatsApp

Math Methods

The Math object methods enable us to perform tasks on one or more numbers. 1. abs() The JavaScript's Math abs() method returns the absolute value of a number. An absolute value is the positive value of a number be it positive or negative. This means when a negative value is passed to the method, it returns its positive equivalent.
<script>
document.write(Math.abs(-315)); //returns 315
</script>

Copy PRACTICE IT

2. acos() Returns in radians, the arccosine of a number.
<script>
document.write(Math.acos(1));
</script>

Copy PRACTICE IT

3. asin() Returns in radians, the arcsine of a number.
<script>
document.write(Math.asin(1));
</script>

Copy PRACTICE IT

4. atan() Returns in radians, the arctangent of a number.
<script>
document.write(Math.atan(1));
</script>

Copy PRACTICE IT

5. ceil() Rounds a value up to its nearest integer. This means once a value have a decimal point and any value other than zero (0) e.g 3.01, it rounds it to 4.
<script>
document.write(Math.ceil(3.4) + Math.ceil(1.5) + Math.ceil(2.8)); //returns 4+2+3 = 9
</script>

Copy PRACTICE IT

6. cos() Returns the cosine of a number.
<script>
document.write(Math.cos(1));
</script>

Copy PRACTICE IT

7. exp() Raises the Euler's number(2.718281828459045) to the power of the number specified in its parameter.
<script>
document.write(Math.exp(10)); //Raises Euler's number to the power of 10
</script>

Copy PRACTICE IT

8. floor() Rounds a value down to its nearest integer. This means it takes off the decimal part of a number.
<script>
document.write(Math.floor(3.4) + Math.floor(1.5) + Math.floor(2.8)); //returns 3+1+2 = 6
</script>

Copy PRACTICE IT

9. log() Returns the natural logarithm (base E) of any number.
<script>
document.write(Math.log(10));
</script>

Copy PRACTICE IT

10. max() Returns the highest value among a list of numbers assigned to its parameters.
<script>
document.write("The highest value is "+Math.max(1, 10, 3, 15, 21, 5));
</script>

Copy PRACTICE IT

11. min() Returns the lowest value among a list of numbers assigned to its parameters.
<script>
document.write("The lowest value is "+Math.min(1, 10, 3, 15, 21, 5));
</script>

Copy PRACTICE IT

12. pow() Raises a number to the power of another. It's first argument is the number to be raised to the power of the second argument e.g pow(5, 2) will raise 5 to the power of 2.
<script>
alert(Math.pow(3,2)); //raises 3 to the power of 2
</script>

Copy PRACTICE IT

13. random() Returns a random number from 0 to 1. It may return 0 or just any decimal number lying between 0 to 1 e.g 0.45 but not 1.
<script>
document.write(Math.random());
</script>

Copy PRACTICE IT

To get a random number base on your own specifications e.g a random number from 0 to 10 but 10 not inclusive. You simply multiply 10 with the random() method.
<script>
document.write(Math.random() * 10); //random number from 0 to 10 but not 10
</script>

Copy PRACTICE IT

What happens then is whatever random number is gotten from 0 to 1 is multiply by the specified number (10) to get another value. To get a non - decimal value, you can thereafter floor() the random number generated.
<script>
document.write(Math.floor(Math.random() * 100)); //non - decimal random number from 0 to 100
</script>

Copy PRACTICE IT

To specify a minimum value to start randomizing from, you may want to define a custom function as shown below,
<script>
function randomFrom(start, end){
var randomNum = (Math.random() * (start - end)) + end;

return randomNum;
}

document.write(Math.floor(randomFrom(3, 6))); //returns the random number from 3 to 6
</script>

Copy PRACTICE IT

14. round() This method rounds a number to it nearest integer. To do this, it pays attention to the digit after the decimal point. If the digit is 5 or greater than 5, it adds 1 to the number before the decimal point else it adds nothing.
<script>
document.write("You get "+Math.round(4.15)+" when you round 4.15.<br/><br/>You get "+Math.round(4.5)+" when you round 4.5.");
</script>

Copy PRACTICE IT

15. sin() This method returns the sine of a number.
<script>
document.write(Math.sin(1));
</script>

Copy PRACTICE IT

16. sqrt() Returns the square root of a number.
<script>
document.write(Math.sqrt(25)); //square root of 25
</script>

Copy PRACTICE IT

17. tan() Returns the tangent of a number.
<script>
document.write(Math.tan(5));
</script>

Copy PRACTICE IT

SUMMARY
  1. In this page, we listed the various methods of the Math object.
  2. They allow you to manipulate numbers. For example, instead of having to devise and define a function that will help you get the square root of any number, you can make use of the sqrt() method of the Math object.
  3. There are other numerous methods.
  4. Remember you don't need to create an object of the Math to start using its methods. It is already a literal object and not a prototype.
Take a short quiz on Math Methods.
Ask a question or tell us if Math Methods is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode