<<

| JAVASCRIPT BASICS

JavaScript Math Functions

Functions

 The Math functions helps you to perform mathematical tasks in a very way and lot of inbuilt mathematical functions which

makes the programmers life easier. Typical example is, Math. min which will provide you the minimal value among the listed values provided in a array of .

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | JAVASCRIPT BASICS

Below are some of the math functions

Function Name What it does? Math.min() This function returns the minimal value in the list of numbers Math.min(50, 60, 30, 20, -18, -4); The output is -18

Math.max() This function returns the mamimum value in the list of numbers Math.min(50, 60, 30, 20, -18, -4); The output is 60 Math.round() This function provides the off option to the This function rounds off to the nearest number Math.round(6.7); // returns 7 Math.ceil() This function provides the rounding off option to the number. It will rounds off to the top nearest number Math.round(6.7); // returns 7 Math.floor() This function provides the rounding off option to the number. It will rounds off to the bottom nearest number Math.round(6.7); // returns 6 Math.E This Math function will returns Euler’s number Math.PI This Math function will returnsPI Math.SQRT2 This Math function will returns the of 2 Math.SQRT1_2 This Math function will returns the square root of 1/2 Math.LN2 This Math function will returns the natural of 2 Math.LN10 This Math function will returns the natural logarithm of 10 Math.LOG2E This Math function will returns base 2 logarithm of E Math.LOG10E This Math function will returns base 10 logarithm of E

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | JAVASCRIPT BASICS

In addition to this, there are lot of mathematical methods which you can use it in your JavaScript code.

Method Description abs(val) This method returns the absolute value of val acos(val) This method returns the arccosine of val, in radians asin(val) This method returns the arcsine of val, in radians atan(val) This method returns the arctangent of val as a numeric value between -PI/2 and PI/2 radians atan2(y,x) This method returns the arctangent of the quotient of its arguments ceil(val) Returns val, rounded upwards to the nearest cos(val) This method returns the cosine of val (val is in radians) exp(val) This method returns the value of Ex floor(val) Returns val, rounded downwards to the nearest integer log(val) This method returns the natural logarithm (base E) of val max(x,y,z,...,n) This method returns the number with the highest value min(x,y,z,...,n) This method returns the number with the lowest value pow(x,y) This method returns the value of x to the power of y random() Returns a random number between 0 and 1 round(val) Rounds val to the nearest integer sin(val) This method returns the of val (val is in radians) sqrt(val) This method returns the square root of val tan(val) This method returns the tangent of an angle

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | JAVASCRIPT BASICS

Sample code

WikiTechy. JavaScript Math Function. Math.random() returns a random number between 0 and 1.

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | JAVASCRIPT BASICS

Code Explanation:

Paragraph tag displaying the text “WikiTechy - JavaScript String Functions” Button tag with onclick event placed. On clicking of button the onclick will call the function myFunction() Button tag enclosed with the events.

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | JAVASCRIPT BASICS

Paragraph tag in which we are trying to get the result. The id of the paragraph tag is,” WikiTechy_Text”

Script tag enclosed the JavaScript tag. The format is

Function method is declared at this stage. function myFunction () { document.getElementById("WikiTechy_Text").innerHTML = Math.random(); }

Trying to fetch the element in the HTML “WikiTechy_Text” and accessing the innerHTML value and assign the value returned by Math.random() function.

Math.random() function will return a value between 0 and 1.

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | JAVASCRIPT BASICS

Sample Output:

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | JAVASCRIPT BASICS

Output:

Displaying the text “WikiTechy - JavaScript Operators” in the paragraph tag of the HTML page.

Button created to invoke the onclick event. So that, the button onclick will call the function myFunction()

Displaying the result of the variable with the Math.random() function executing.

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | JAVASCRIPT BASICS

On pressing the button once again

Displaying the text “WikiTechy - JavaScript Operators” in the paragraph tag of the HTML page.

Button created to invoke the onclick event. So that, the button onclick will call the function myFunction()

Displaying the result of the variable with the Math.random() function executing. Now, the data is changed once again and the values will be between 0 and 1.

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | JAVASCRIPT BASICS

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved.