Math Objects In JavaScript

Math object provides many properties and functions which allows to perform complex mathematical work. A Math object is a built in object of JavaScript using which a lot of mathematical calculations can be performed. We can use these mathematical functions to create the animation in JavaScript also
 
Math object properties
E- this property returns Euler’s Constant (rougly 2.718)
PI- this property returns the value of PI
Length – property returns the no of characters present in string.
 
Math object Methods
 
abs() – this function calculates and returns the absolute value.
ceil() – this function returns the next greater or equal integer..
cos() – this function is used to calculate the cosine number..
sin()– this function is used to calculate the sin() of a number.
random () – this function returns a randomly calculated number between 0 and 1. .
sqrt() – this function calculates and returns the square root of a number.
tan() – this function calculates and returns tangent of a number.
floor() – this function returns next integer lesser than or equal to a number..
pow() – this function returns calculated value of one number for given number of powers.
 
bold() – returns the string formatting to bold.
italics() – returns the string formatting italics.
charAt() – returns the character present in string at given position.
substring() – returns the characters present from given start position to given end position.
toLowerCase() – returns the string converted to lowercase letters.
toUpperCase() – returns the string converted to uppercase letters.
 
Math Object’s abs() function
 
This function calculates and returns the absolute value. Suppose the value of any variable is –345 the absolute value of –345 will be 345. if the value of a variable is 345 then the absolute value of variable will be 345.
 
Example:
 
<html>

<head>

</head>

<body>

<script>

var a=-45

document.write(“<br> Value of a :”+a)

document.write(“<br> Absolute Value of a :”+Math.abs(a))

</script>

</body>

</html>

click here to view the results of this program in browser
 
 
Math Object’s ceil() function
 
ceil()- this function returns the next greater or equal integer. If any number is having a decimal value then the value is increased with one else it remains as it is. Fox example a variable is having value 45.26 then the ceiling value of this variable will be 45 but if the variable value is 25 the ceil value will be 25 only.
 
Example:
 
<html>

<head>

</head>

<body>

<script>

document.write(“<br>Ceiling Number is : “+Math.ceil(45.26))

document.write(“<br>Ceiling Number is : “+Math.ceil(3.22))

document.write(“<br>Ceiling Number is : “+Math.ceil(25))

</script>

</body>

</html>

click here to view the results of this program in browser
 
 
Math Object’s cos() function
 
cos()- this function is used to calculate the cosine number. Math object contains cos() function which can return the cos value of the angle provided with the cos function.
 
Examples:
 
<html>

<head>

</head>

<body>

<script>

var a=0

document.write(“<br> Cosine of a is : “+Math.cos(a))

document.write(“<br> Cosine of 90 is : “+Math.cos(90))

document.write(“<br> Cosine of 180 is : “+Math.cos(180))

</script>

</body>

</html>

click here to view the results of this program in browser
 
 
Math Object’s floor() function
 
floor()- this function returns next integer lesser than or equal to a number. If any value is having the decimal values with it floor function provides the decimal truncated value. Fox example a variable is having value 45.26 the floor value of that variable will be 45 but if the value is 32 then the floor value of variable will be 32 only.
 
Example:
 
<html>

<head>

</head>

<body>

<script>

document.write(“<br>Floor Number is : “+Math.floor(45.26))

document.write(“<br>Floor Number is : “+Math.floor(3.22))

document.write(“<br>Floor Number is : “+Math.floor(26.23))

</script>

</body>

</html>

click here to view the results of this program in browser
 
 
Math Object’s pow() function
 
pow()- this function returns calculated value of one number for given number of powers. For example if the parameters passed to the function are 2 , 3 then the answer will be 8 , 2*2*2 = 8 , this function requires the base and the power of the value and returns the calculated value.
 
Example:
 
<html>

<head>

</head>

<body>

<script>

var a=2

var b=3

document.write(“<br> Value of 2’s power 3 is : “+Math.pow(2,3))

document.write(“<br> Value of 3’s power 4 is : “+Math.pow(3,4))

document.write(“<br> Value of 4’s power 3 is : “+Math.pow(4,3))

</script>

</body>

</html>

click here to view the results of this program in browser
 
 
Math Object’s random() function
 
random ()- this function returns a randomly calculated number between 0 and 1. The Math object contains a random function which can be used to get the randomly generated value between 0 & 1, The code given below will generated a random value but if refreshed every time it will generate a new randomly generated value.
 
Example:
 
<html>

<head>

</head>

<body>

<h5> On Refresing screen every time new number between 0 & 1 <br> will be generated by random function</h5>

<script>

document.write(“<br>Random Number is : “+Math.random())

</script>

</body>

</html>

click here to view the results of this program in browser
 
 
Math Object’s sin() function
 
sin()- this function is used to calculate the sin() of a give number. Math object contains sin function which can be used to get the sin value of any angle given to sin function as parameter. In the below given example we are passing different angle values to sin function which will generate different values
 
Example:
 
<html>

<head>

</head>

<body>

<script>

var a=0

document.write(“<br> sine of a is : “+Math.sin(a))

document.write(“<br> sine of 45 is : “+Math.sin(45))

document.write(“<br> sine of 60 is : “+Math.sin(60))

document.write(“<br> sine of 90 is : “+Math.sin(90))

</script>

</body>

</html>

click here to view the results of this program in browser
 
 
Math Object’s sqrt() function
 
sqrt()- this function calculates and returns the square root of a number. Math object has a sqrt function used to get the square root of the number given with function. Fox example if the variable value or value given to sqrt function is 81 then it will return 9 because the square root of 81 is 9.
 
Example:
 
<html>

<head>

</head>

<body>

<script>

var a=4

document.write(“<br>Square Root of a is : “+Math.sqrt(a))

document.write(“<br>Square Root of 81 is : “+Math.sqrt(81))

document.write(“<br>Square Root is 49 is : “+Math.sqrt(49))

</script>

</body>

</html>

click here to view the results of this program in browser
 
 
Math Object’s sqrt() function
 
tan()- this function calculates and returns tangent of a number. This function will return the value generated of the angle given with tan function.
 
Example:
 
<html>

<head>

</head>

<body>

<script>

var a=0

document.write(“<br> angent of a is : “+Math.tan(a))

document.write(“<br> tangent of 45 is : “+Math.tan(45))

document.write(“<br> tangent of 60 is : “+Math.tan(60))

document.write(“<br> tangent of 90 is : “+Math.tan(90))

</script>

</body>

</html>