- This object allows mathematical tasks to be performed on numbers.
- This object has no constructor and cannot be created first.
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.min() to find the lowest:
Math.min(65, 951, 84, 20, -98, -235);
Math.max(65, 951, 84, 20, -98, -235);
Math.random() will return a random numeric value between 0 (inclusive), and 1 (exclusive):
Math.round() is used to round a number to the nearest integer, up or down:
Math.ceil() is used to round a number up to the nearest integer:
Math.floor() is used to round a number down to the nearest integer:
Get a Random Number
You can use
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:
Math.floor(Math.random() * 10);
Theory is great, but we recommend digging deeper!
Properties for Constants
|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.
|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|
Mathobject has many methods and properties you can use.
- You can use
Mathobject to get a random number.