JavaScript - Math




JavaScript - Math

The Math object in Javascript is said to be static. One cannot create new instances of the object and all the properties and functions can be accessed using the dot operator and by passing the operators as arguments to the functions.

The JavaScript Math Object might not be used quite as often as the string object, but it is really useful when dealing with numbers. Sometimes we need to find the nearest integer to a number with a decimal (float). Other times, we need to do some sophisticated mathematics on them, such as powers or square roots. The math object is also where random number generation resides. number in JavaScript are really simple and so is the Math Object. However, I have sort of mislead you on numbers and the Math Object.

See, the Math Object acts upon numbers, they do not utilize the Math Object as a method. As you will see in the examples below, we must perform the Math methods on our numbers. This is why you will see Math.method(number) being used in the references below. I know it seems like once you have a number it should have methods that do these Math Object Methods, but they simply don’t exist.

Common Math Object Methods

abs(x) – gets absolute value of x

Example

document.write(Math.abs(-1));

Result

1

1
ceil(x) – gets x rounded up to the nearest integer

Example

document.write(Math.ceil(2.6));

Result

3

3
floor(x) – gets x rounded up to the nearest integer

What is an JavaScript Math ?

JavaScript math allows you to easily perform mathematical tasks like returning a random number, or rounding up or down. How can you use JavaScript math to help further the impact an expression has on an object? Author Luisa Winters demonstrates how to use a few of the JavaScript math options that are included in Adobe After Effects.

JavaScript math allows you to easily perform…mathematical tasks like returning a random number,…or rounding up or down.…Let's try some of the ones that are included…in the expression language menu.…Let's go ahead and open composition 4.4 JavaScript Math.…In here you can see a green ball.…Let's go ahead and add an expression…to the position of this ball.…So, select the layer, press the letter p.…Let's go ahead and add an expression here. Now the expression is going to affect…only the up and down of the movement of the ball.…So let's go ahead and type some conditionals.…a space equals space,…b space equals space,…and c space equals space.…Let's make this a little bit taller…so we can see it better.…a, we're just gonna point it at itself.…b, we're gonna point it at itself.…And c is gonna put both of them together.…So open and close the brackets,…and now inside, we're gonna type a,b…After these, don't forget to put a semi-colon. Remember the semi-colon is the equivalent…of a period in English.…Now, let's go ahead and wiggle just the y.…

In JavaScript, Math is a built-in object. It has both properties and methods for mathematical functions and constants. Math operates with the Number type, but never with BigInt. Unlike other global objects, Math is not considered a constructor. The methods and properties of Math are fixed. You can call them by applying Math as an object without creating it. So, you refer to constant pi as Math.PI. Hence, the sine function should be called Math.sin(x). Here, x is the argument of the method. The syntax to call the methods and properties of Math is the following -

var pi_value = Math.PI;
var sin_value = Math.sin(30);

JavaScript math object contains various built-in methods and mathematical constants for performing mathematical calculations in scripts. The Math object of JavaScript allows you to perform certain calculations by using method functions of the Math object. It provides a few constants such as pi, log values and exponential values. For this we have to define a variable and set its value through a property or function of the Math Object. If you want to use a math property which returns a constant value, then you have to write like this, for getting value of pi in a simple variable you would have to use.

[ var value=math.property ] [var pi_value=Math.pi ] //This variable return value of pi 3.14 [var my_money=Math.sqrt(x);]

Most of the member functions have one or more parameters, which is what the "x" is for. You can replace x with a number or variable. For instance, if you want the square root of a number, you can call the square root member function of the Math object with the number as the parameter.

[ var my_money=Math.sqrt(16); ] 

This gives back the square root of 4. There are few rules and regulation for using math object; you have to remember while using math object. The "M" in Math is always capitalized. The word Math is followed by the dot operator (.), and then the property or member function you want to use. Here we will deal with some most common method and properties.

Script to finding round off of a number

<html> 
<head> 
<title>JavaScript Math Object</title> 
</head> 
 <body> 
  <script type="text/javascript"> 
  document.write(Math.round(16.12345) + "<br />") 
  document.write(Math.round(25.0)+ "<br />") 
  document.write(Math.round(36.111) + "<br />") 
  document.write(Math.round(49.992222233) + "<br />") 
  </script> 
</body> 
</html>

JavaScript Math Object

Math is an object inbuilt in the JavaScript library that has various methods we as a programmer can use for carrying out mathematical operations. To work on JavaScript and use its functions without downloading the extra compiler and installing in your system follow these steps. Open the chrome dev console to try out the examples by right-clicking on the browser → selecting inspect → selecting console or simply type f12. You can see various properties attached to this object by simply logging out on the console Math.

console.log(Math);

It also contains very precise values of some mathematical constants such as pi, Euler's number, square root of 2, etc. It's pretty simple to use it. Let's start by seeing some constants on the console.

console.log(Math.PI);
console.log(Math.LOG2E);
console.log(Math.LN2);

Math.round() method

We can use the round() method of this object to round any number to the nearest integer.

console.log(Math.round(5.2)); //5
console.log(Math.round(5.7)); //6
console.log(Math.round(0.1)); //0

Math.floor() method

Much similar to round is floor(), which gives an integer smaller than the number passed to it.

console.log(Math.floor(2.33)); //2
console.log(Math.floor(2.99)); //2
console.log(Math.floor(11.74)); //11

Math.ceil() Method

ceil() is something that does totally opposite of floor. It gives an integer greater than the number passed to it.

console.log(Math.ceil(43.44)); //44
console.log(Math.ceil(9.1)); //10
console.log(Math.ceil(12.8)); //13

Math.trunc() Method

If we simply want the integer part of the number and neglect the decimal, we can use trunc().

console.log(Math.trunc(5.667)); //5
console.log(Math.trunc(0.9359)); //0

Math.random() Method

Math.random() gives us a random number between 0 and 1. Every time the method is invoked, a new random number is generated.

console.log(Math.random());

Okay so now let's build a simple program to see where we can utilize Math objects in real-world scenarios. Let's say we have to generate a random number for a lottery. We have to generate a random winning ticket in a particular range. We can set the range to initially a smaller value to test our lottery app. All you need is an index.html so go ahead and create an index.html file in any folder and copy the following started template,

All we have done till now is created something that looks like this -

  <html> 
  <head> 
    <title>JavaScript Math Object: Simple Computations and Constants</title> 
   <style> 
     body{font-family:Helvetica;}
   </style>
  <script>
  var value = 25;
    document.write("The log of the number is 25 <b>" + Math.log(value)+ "</b></br>");
  document.write("The square root of the number 25 is <b>" + Math.sqrt(value)+ "</b></br>");
  document.write("The value of Constant PI is <b>" + Math.PI+ "</b></br>");
    document.write("The number 25 raised to the power of 2 is <b>" + Math.pow(value,2)+ "</b></br>");
  
  </script>
  </head> 
   <body> 
   </body>
</html>

Output

The log of the number is 25 3.2188758248682006
The square root of the number 25 is 5
The value of Constant PI is 3.141592653589793
The number 25 raised to the power of 2 is 625

Math Properties

The following table lists the standard properties of the Math object.

For Example
Property Description
E Returns Euler's number, the base of natural logarithms, e, approximately 2.718
LN2 Returns the natural logarithm of 2, approximately 0.693
LN10 Returns the natural logarithm of 10, approximately 2.302
LOG2E Returns the base 2 logarithm of e, approximately 1.442
PI Returns the ratio of the circumference of a circle to its diameter (i.e. π). The approximate value of PI is 3.14159
LOG10E Returns the base 10 logarithm of e, approximately 0.434
SQRT1_2 Returns the square root of 1/2, approximately 0.707
SQRT2 Returns the square root of 2, approximately 1.414

Note - The Math object is just a collection of static functions and constants. The Math object is different from other built-in objects (e.g. Date, Array, String, etc.), because it has no constructor, so there is no way to create an instance of Math.