JAVASCRIPT BASICS
- Introduction to JavaScript
- Javascript Features
- JavaScript in HTML
- JavaScript Syntax
- Output in JavaScript
- JavaScript Variables
- Javascript let Keyword
- JS Literals and Keywords
- JavaScript Data Types
- JavaScript Operators
- JavaScript Events
- Javascript const Keyword
JAVASCRIPT FLOW CONTROL AND LOOPS
- JS if, else and else if
- JavaScript for Loop
- JS while and do-while loop
- JS Switch case
- JS break and continue
JAVASCRIPT ADVANCED
- JavaScript Strings
- JavaScript String Methods
- JavaScript Array
- JavaScript Regular Expression
- JavaScript Popup Boxes
- JavaScript Functions
- JavaScript Default Parameters
- JavaScript Rest Parameters
- JavaScript Arrow Function
- JavaScript Errors
- JavaScript Error Handling
- JavaScript Cookies
JAVASCRIPT OBJECT
- JavaScript Objects
- JavaScript User-defined Object
- JavaScript Boolean Object
- JavaScript Number Object
- JavaScript Math Object
- JavaScript Date Object
- JavaScript RegExp Object
JAVASCRIPT WEB API AND INTERFACES
In JavaScript, Math is a built-in object which includes properties and methods for mathematical operations. We can use the Math object to perform simple and complex arithmetic operations.
Note: Math works with the JavaScript Number type only.
In Math object, All the properties and methods are static. So, we don’t need to create its object to use its property or method. Also, even if we want, we cannot create an object as Math is not a constructor function.
Using JavaScript Math
Suppose, we want to get the value of PI for geometric calculation or the square root of a number in our program then we can use Math object. Let’s see how:
let pi = Math.PI;
document.write(pi + "<br/>");
let sqrt = Math.sqrt(9);
document.write(sqrt);
3.141592653589793 3
JavaScript provides a rich set of Math properties that can be used to get predefined constant values.
JavaScript Math Properties
Following are the properties provided by the JavaScript Math object:
Property | Description |
---|---|
E | holds Euler number whose value is 2.718(approx) |
LN2 | holds a natural logarithm of 2 whose value is 0.693(approx). |
LN10 | holds a natural logarithm of 10, whose value is 2.302(approx). |
LOG2E | holds the base-2 logarithm of E have value1.442(approx) |
LOG10E | holds the base-10 logarithm of E having value 0.434(approx) |
PI | holds the numerical value of PI, whose approx value is 3.142 |
SQRT1_2 | holds the square root of 1/2, whose approx value is 0.707 |
SQRT2 | holds the square root of 2, having approx value 1.414 |
Let’s take an example to see how we can use these properties.
var e = Math.E
document.write(e +"<br>")
var ln2 = Math.LN2
document.write(ln2 +"<br>")
var ln10 = Math.LN10
document.write(ln10 +"<br>")
var sq = Math.SQRT1_2
document.write(sq +"<br>")
2.718281828459045 0.6931471805599453 2.302585092994046 0.7071067811865476
Find Minimum and Maximum Number
Suppose, we want to find min and max numeric value from the random number of list then it can be very easy by using Math object. See the below example.
// Find minimum number
var min = Math.min(10,25,47,23,18,8,21,30)
document.write(min +"<br>")
// Find maximum number
var max = Math.max(10,25,47,23,18,8,21,30)
document.write(max +"<br>")
8 47
JavaScript Math Methods
JavaScript Math Object provides a rich set of methods that are used to make math calculation easy and helps to reduce effort and time in math-oriented programming. Here we have listed the most commonly used Math object methods:
Method | Description |
---|---|
abs(x) | returns the absolute value of x |
ceil(x) | rounds up x to a nearest biggest integer |
cos(x) | returns the cosine value of x |
exp(x) | returns the value of the exponent. |
random() | returns a random number between 0 and 1 |
tan(x) | returns the tangent value of x |
sqrt(x) | returns the square root of x |
sin(x) | returns the sine value of x. |
floor(x) | rounds up x to the nearest smallest integer. |
max(x,y,z,......n) | returns the highest number from the lsit. |
min(x,y,z,.......n) | returns the lowest number from the list. |
pow(x,y) | returns x to the power of y |
cos(x) | returns the cosine value of x |
log(x) | returns the logarithmic value of x |
<html>
<head>
<title>
Using Math Functions
</title>
</head>
<body>
<script type="text/javaScript">
document.write("Floor :"+Math.floor(12.7)+"<BR/>");
document.write("Log :"+Math.log(12.7)+"<BR/>");
document.write("Max :"+Math.max(12.7,11,25,67)+"<BR/>");
document.write("Min :"+Math.min(3,78,90,12.7)+"<BR/>");
document.write("pow :"+Math.pow(10,2)+"<BR/>");
document.write("Random :"+Math.random()+"<BR/>");
document.write("Round :"+Math.round(12.7)+"<BR/>");
document.write("Sin:"+Math.sin(45)+"<BR/>");
document.write("Sqrt :"+Math.sqrt(12.7)+"<BR/>");
document.write("Exp:"+Math.exp(10)+"<BR/>");
document.write("Tan:"+Math.tan(45)+"<BR/>");
</script>
</body>
</html>
So to use Math object properties or methods, we do not have to create an object using the new
keyword, and we can directly use the properties and methods in our code.
3 thoughts on “JavaScript Math Object”