Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JavaScript 013 – JavaScript Math Object Functions

1. JavaScript Math Object

The Math Object, as the name suggests, offers functions to perform calculations. Unlike other JavaScript object, you no need to create Math object to access its functionality as it is available in every JavaScript. What is the need for Math functions? For example, it is hard to achieve trigonometric functions via simple arithmetic operators (+,-,* and /) and a method like tan() will do. The Math also defines constants like Pi, which one can use in area calculations. In this example, we will explore some frequently used Math Object functions.

2. Math Object – abs, min, max

In this section, we will explore the JavaScript Math Object functions abs, min, and max. Have a look at the below example:

JavaScript Math Functions abs, Min, Max
JavaScript Math Functions abs, Min, Max

1) The Math function abs can accept a positive or negative number. But it converts the negative number into positive and returns the result. Here, we passed -12 and the abs function returned +12.
2) JavaScript’s Math Object’s min function can accept variables number of parameters and find the minimum among them. In our example, the function returns -7 as a minimum value among the passed-in numbers.
3) Like min, the max will find the maximum value among the passed in parameters and returns that. In our case, we got 21 as a result.

3. Rounding Functions in Math

When there is a decimal number and you want to have a fixed decimal position, rounding comes into the picture. Here in the below example, we explored the functions – parseInt, ceil, floor and round.

JavaScript Rounding Functions
JavaScript Rounding Functions

1) The ceil function moves to the next whole number regardless of the value in the decimal points. Here, we get the value of 12 for the number 11.08
2) Conversion function parseInt also performs the duty, but it wipes the decimal part of the number and gives the result as 11.
3) You may think ceil will give you -12 here. But, the result will be -11 as it is the next whole number. Remember this, when sorted:  -12, -11.08, -11,-10….0….10,11,11.08,12,13,14.
4) Again, parseInt won’t consider about rounding giving you -11.
5) The floor function returns the whole number moving in backward direction. For example, 11.08 will give you 11. Now compare this ceil function @ 1.
6) This will show you how floor math function works on negative numbers.
7, 8, 9, 10) The round function works based on the value in the decimal portion. When the decimal number is 0.5 & plus, the next high whole number will be returned. For example, 11.52 is 12 & 11.44 is 11. The same is applicable for negative numbers as well. The Math object’s round function performs the same way for negative numbers as well.

4. Math Object – pow & random functions

JavaScript’s Math object random function will generate random numbers. The pow function will calculate the power of a number. Have a look at the below example:

JavaScript Power Function and Random Numbers
JavaScript Power Function and Random Numbers

1) The pow function accepts two parameters. First parameter is the base and second is the power. In the first part of the example, we run a for loop and generate the square of the numbers from 1 through 10.
2) JavaScript’s random function, when called, gives a value between 0 to 1 in fractions. We can multiply this fraction with any number to get the range of random numbers. In the second part of the example, we run a for loop for 10 times and call the random() function on each iteration. Since we multiply the return value with 100, we generate the random numbers between 0 to 100.

5. Code Reference

Categories: JavaScript

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.