Code has been added to clipboard!

Main Tips on Using JavaScript Math Functions in a Nutshell

Reading time 4 min
Published Aug 8, 2017
Updated Oct 1, 2019

JavaScript math Functions are built-in and static. They allow developers to implement not only basic mathematical operations but advanced calculations as well.

Working with JavaScript built-in math object can be highly useful, whether you want to build a calculator for your website or generate a random number for a guessing game. In this tutorial, you will learn to work with the JavaScript math object. You will learn all about its syntax, the most popular methods and properties.

JavaScript Math Object: Main Tips

  • This object allows mathematical tasks to be performed on numbers.
  • The JavaScript Math object includes many methods and properties you can apply.
  • This object has no constructor and cannot be created first.

Math Functions

In this chapter, we will present you with the most commonly used math functions. Each of them will be explained in a short and clear manner for you to grasp each concept quickly. Code examples will be helpful, too. If you wish to read more on math functions, don't hesitate to visit this tutorial we have created for easy referral.

Math.max() and Math.min()

You can use Math.max() JavaScript to find the highest out of listed values, and Math.min() to find the lowest:

Example
Math.min(65, 951, 84, 20, -98, -235);
Example
Math.max(65, 951, 84, 20, -98, -235);

Math.random()

Math.random() will return a random numeric value between 0 (inclusive), and 1 (exclusive):

Example
Math.random();

Math.round()

JavaScript Math.round() is used to round a number to the nearest integer, up or down:

Example
Math.round(5.8);
Math.round(3.2);

Math.ceil()

Math.ceil() is used to round a number up to the nearest integer:

Example
Math.ceil(5.8);
Math.ceil(3.2);

Math.floor()

JavaScript Math.floor() is used to round a number down to the nearest integer:

Example
Math.floor(5.8);
Math.floor(3.2);

Get a Random Number

You can use Math.random() and Math.floor() together to get a random integer ranging from 0 to 10.

In the example below, Math.random() generates a random decimal number between 0 and 1, which is multiplied by 10 to get a decimal number between 0 and 10. Then, Math.floor() method rounds that number down to an integer:

Example
Math.floor(Math.random() * 10);

Properties for Constants

Math object properties are used for math constants. You are probably familiar with some of them, like the Pi number, or Euler's number. In cases when you need them in your JavaScript code, you might use the properties listed in the tables below. You will find both names of the constant and approximate values defined for each one.

Property Description
E Return Euler's number (approximately 2.718)
LN2 Return natural logarithm of 2 (approximately 0.693)
LN10 Return natural logarithm of 10 (approximately 2.302)
LOG2E Return base-2 logarithm of E (approximately 1.442)
LOG10E Return base-10 logarithm of E (approximately 0.434)
PI Return PI (approximately 3.1416)
SQRT1_2 Return square root of 1/2 (approximately 0.707)
SQRT2 Return square root of 2 (approximately 1.414)

Methods To Use

You can use various methods for working with math objects, too. They will let you get arithmetic as well as trigonometric values. See the table below to get familiarized with them. As you learn to use these methods, working with math objects will be a breeze.

Method Description
abs(var1) Return absolute value of var1
acos(var1) Return arccosine of var1 in radians
asin(var1) Return arcsine of var1 in radians
atan(var1) Return arctangent of var1 as numeric value between -PI/2 and PI/2 radians
atan2(var2,var1) Return arctangent of the quotient of arguments
ceil(var1) Return var1 rounded upwards to nearest integer
cos(var1) Return cosine of var1 (var1 is in radians)
exp(var1) Return value of Evar1
floor(var1) Return var1 rounded downwards to nearest integer
log(var1) Return natural logarithm (base E) of var1
max(var1,var2,var3,...,n) Return number with highest value
min(var1,var2,var3,...,n) Return number with lowest value
pow(var1,var2) Return value of var1 to power of var2
random() Return random number between 0 and 1
round(var1) Round var1 to nearest integer
sin(var1) Return sine of var1 (var1 is in radians)
sqrt(var1) Return square root of var1
tan(var1) Return tangent of angle

JavaScript Math Object: Summary

  • Math object allows you to perform JavaScript math functions.
  • The Math object has many methods and properties you can use.
  • You can use Math object to get a random number.
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen