JavaScript Math object Examples and Reference

   Math is a built-in JavaScript object that has properties and methods to perform mathematical tasks. The Math object cannot be created using the new operator. It has no constructor defined. All the Properties and Methods of Math object are static. That means we no need to create object for it, we can directly access the properties and methods using Math.

JavaScript Math Properties or Constants


JavaScript Math Object has 8 Constants, They are

Math.E 

          Euler's constant and the base of natural logarithms, approximately 2.718.
Ex:-
var EulerVal = Math.E;
console.log("Euler's Value is : " + EulerVal); 
  
Output is:
  Euler's Value is : 2.718281828459045 

Math.LN2 

          This returns natural logarithm of 2
Ex:-
var Val = Math.LN2;
console.log("Val's Value is : " + Val); 
  
Output is:
  Val's Value is : 0.6931471805599453 

Math.LN10 

          This returns natural logarithm of 10
Ex:-
var Val = Math.LN10;
console.log("Val's Value is : " + Val); 
  
Output is:
  Val's Value is : 2.302585092994046 

Math.LOG2E 

          This returns base 2 logarithm of E
Ex:-
var Val = Math.LOG2E;
console.log("Val's Value is : " + Val); 
  
Output is:
  Val's Value is : 1.4426950408889634 

Math.LOG10E 

          This returns base 10 logarithm of E
Ex:-
var Val = Math.LOG10E;
console.log("Val's Value is : " + Val); 
  
Output is:
  Val's Value is : 0.4342944819032518 

Math.PI 

          This returns the ratio of the circumference of a circle to its diameter
Ex:-
var Val = Math.PI;
console.log("Val's Value is : " + Val); 
  
Output is:
  Val's Value is : 3.141592653589793 

Math.SQRT2 

          Square root of 2
Ex:-
var Val = Math.SQRT2;
console.log("Val's Value is : " + Val); 
  
Output is:
  Val's Value is : 1.4142135623730951 

Math.SQRT1_2 

          Square root of 1/2
Ex:-
var Val = Math.SQRT1_2;
console.log("Val's Value is : " + Val); 
  
Output is:
  Val's Value is : 0.7071067811865476 

JavaScript Math Object Methods


Below are the list of JavaScript Math Object methods their use and examples.

Math.abs(x) 

          This method returns the absolute value of a number(x)
Ex:-
var val = Math.abs(-1);
console.log("Value is : " + val); 

val = Math.abs(0);
console.log("Value is : " + val); 

val = Math.abs(2);
console.log("Value is : " + val); 
  
Output is:
Value is : 1
Value is : 0
Value is : 2

Math.acos(x) 

          This method returns returns the arccosine of x, in radians
Returns a numeric value between 0 and PI radians for x between -1 and 1. If the value of number is outside this range, it returns NaN. Ex:-
var val = Math.acos(-1);
console.log("Value is : " + val); 

val = Math.acos(0);
console.log("Value is : " + val); 

val = Math.acos(2);
console.log("Value is : " + val); 
  
Output is:
Value is : 3.141592653589793
Value is : 1.5707963267948966
Value is : NaN

Math.asin(x) 

          This method returns the arcsine of x, in radians
Returns a numeric value between -pi/2 and pi/2 radians for x between -1 and 1. If the value of number is outside this range, it returns NaN. Ex:-
var val = Math.asin(-1);
console.log("Value is : " + val); 

val = Math.asin(0);
console.log("Value is : " + val); 

val = Math.asin(2);
console.log("Value is : " + val); 
  
Output is:
Value is : -1.5707963267948966
Value is : 0
Value is : NaN

Math.atan(x) 

          This method returns the arctangent in radians of a number. The atan method returns a numeric value between -pi/2 and pi/2 radians
Ex:-
var val = Math.atan(-1);
console.log("Value is : " + val); 

val = Math.atan(0);
console.log("Value is : " + val); 

val = Math.atan(2);
console.log("Value is : " + val); 
  
Output is:
Value is : -0.7853981633974483
Value is : 0
Value is : 1.1071487177940904

Math.atan2(x,y) 

          This method returns the arctangent of the quotient of its arguments. The atan2 method returns a numeric value between -pi and pi
Ex:-
var val = Math.atan2(30,60);
console.log("Value is : " + val); 
  
Output is:
Value is : 0.4636476090008061

Math.ceil(x) 

          This method returns the smallest integer greater than or equal to a number
Ex:-
var val = Math.ceil(1.25);
console.log("Value is : " + val); 

var val = Math.ceil(1.55);
console.log("Value is : " + val); 
  
Output is:
Value is : 2
Value is : 2

Math.cos(x) 

          This method returns the cosine of a number. The cos method returns a numeric value between -1 and 1
Ex:-
var val = Math.cos(30);
console.log("Value is : " + val); 

var val = Math.cos(60);
console.log("Value is : " + val); 
  
Output is:
Value is : 0.15425144988758405
Value is : -0.9524129804151563

Math.exp(x) 

         This method returns Ex, where x is the argument, and E is Euler's constant, the base of the natural logarithms
Ex:-
var val = Math.exp(1);
console.log("Value is : " + val); 

var val = Math.exp(2);
console.log("Value is : " + val); 
  
Output is:
Value is : 2.718281828459045
Value is : 7.38905609893065

Math.floor(x) 

         This method returns the largest integer less than or equal to a number
Ex:-
var val = Math.floor(1.25);
console.log("Value is : " + val); 

var val = Math.floor(1.55);
console.log("Value is : " + val); 
  
Output is:
Value is : 1
Value is : 1

Math.log(x) 

         This method returns the natural logarithm (base E) of a number. If the value of number is negative, the return value is always NaN
Ex:-
var val = Math.log(1.25);
console.log("Value is : " + val); 

var val = Math.log(1.55);
console.log("Value is : " + val); 
  
Output is:
Value is : 0.22314355131420976
Value is : 0.4382549309311553

Math.max(val1, val2, ... valN) 

         This method returns the largest of zero or more numbers. If no arguments are given, the results is Infinity
Ex:-
var val = Math.max(1.25, 2.5);
console.log("Value is : " + val); 

var val = Math.max(1, 2, 5);
console.log("Value is : " + val); 

var val = Math.max();
console.log("Value is : " + val); 
Output is:
Value is : 2.5
Value is : 5
Value is : -Infinity

Math.min(val1, val2, ... valN) 

         This method returns the smallest of zero or more numbers. If no arguments are given, the results is Infinity
Ex:-
var val = Math.min(1.25, 2.5);
console.log("Value is : " + val); 

var val = Math.min(1, 2, 5);
console.log("Value is : " + val); 

var val = Math.min();
console.log("Value is : " + val); 
Output is:
Value is : 1.25
Value is : 1
Value is : Infinity

Math.pow(base, exponent ) 

         This method returns the base to the exponent power
Ex:-
var val = Math.pow(1.25, 2.5);
console.log("Value is : " + val); 

var val = Math.pow(1, 2);
console.log("Value is : " + val); 

var val = Math.pow();
console.log("Value is : " + val); 
Output is:
Value is : 1.7469281074217107
Value is : 1
Value is : NaN

Math.random() 

         This method returns a random number between 0 (inclusive) and 1 (exclusive)
Ex:-
var val = Math.random();
console.log("Value is : " + val); 
Output is:
Value is : 0.49192287446931005

Math.round(x) 

         This method returns the value of a number rounded to the nearest integer
Ex:-
var val = Math.round(1.25);
console.log("Value is : " + val); 

var val = Math.round(1.55);
console.log("Value is : " + val); 
Output is:
Value is : 1
Value is : 2

Math.sin(x) 

         This method returns the sine of a number. The sin method returns a numeric value between -1 and 1
Ex:-
var val = Math.sin(1);
console.log("Value is : " + val); 

var val = Math.sin(60);
console.log("Value is : " + val); 
Output is:
Value is : 0.8414709848078965
Value is : -0.3048106211022167

Math.sqrt(x) 

         This method returns the square root of a number. If the value of number is negative, sqrt returns NaN
Ex:-
var val = Math.sqrt(24);
console.log("Value is : " + val); 

var val = Math.sqrt(-24);
console.log("Value is : " + val); 
Output is:
Value is : 4.898979485566356
Value is : NaN

Math.tan(x) 

         This method returns the tangent of a number. The tan method returns a numeric value that represents the tangent of the angle
Ex:-
var val = Math.tan(30);
console.log("Value is : " + val); 

var val = Math.tan(90);
console.log("Value is : " + val); 
Output is:
Value is : -6.405331196646276
Value is : -1.995200412208242

No comments:

Post a Comment