JS Math Object

JavaScript

Share

In this tutorial we will learn about JavaScript Math Object.

We use the Math object for mathematical purposes. Following are some of the properties and methods of the Math object.

Properties of Math object

E

We use the E property to get the value of Euler's constant.

console.log(Math.E);	//this will print 2.718281828459045

PI

We use the PI property to get the value of PI.

console.log(Math.PI);	//this will print 3.141592653589793

LN10

We use the LN10 value of natural log of 10.

console.log(Math.LN10);	//this will print 2.302585092994046

LN2

We use the LN2 value of natural log of 2.

console.log(Math.LN2);	//this will print 0.6931471805599453

SQRT2

We use the SQRT2 value of square root of 2.

console.log(Math.SQRT2);	//this will print 1.4142135623730951

Methods of Math object

abs( )

We use the abs() method to get the absolute value of the number sent as parameter.

var x = 123.456;
console.log(Math.abs(x));	//this will print 123.456

var y = -123.456;
console.log(Math.abs(y));	//this will print 123.456

ceil( )

We use the ceil() method to get the smallest integer value greater than or equal to the number passed as parameter.

var x = 123.456;
console.log(Math.ceil(x));	//this will print 124

var y = -123.456;
console.log(Math.ceil(y));	//this will print -123

floor( )

We use the floor() method to get the largest integer value less than or equal to the number passed as parameter.

var x = 123.456;
console.log(Math.floor(x));	//this will print 123

var y = -123.456;
console.log(Math.floor(y));	//this will print -124

max( )

We use the max() method to get the largest of the two numbers.

var x = 10;
var y = 20;
console.log(Math.max(x, y));	//this will print 20

min( )

We use the min() method to get the smallest of the two numbers.

var x = 10;
var y = 20;
console.log(Math.min(x, y));	//this will print 10

round( )

We use the round() method to get the rounded number to the nearest integer.

var x = 123.456;
console.log(Math.round(x));	//this will print 123

var y = -123.456;
console.log(Math.round(y));	//this will print -123

pow( )

We use the pow() method to get the value of the first parameter raised to the power of second parameter.

var x = 2;
var y = 8;
console.log(Math.pow(x, y));	//this will print 256

exp( )

We use the exp() method to get the value of E to the power of the numeric value sent as parameter.

var x = 2;
console.log(Math.exp(x));	//this will print 7.38905609893065

log( )

We use the log() method to get the value of natural log of the numeric value passed as parameter.

var x = 2;
console.log(Math.log(x));	//this will print 0.6931471805599453

random( )

We use the random() method to get random floating point number in the range [0, 1) i.e., from 0 (inclusive) up to but not including 1 (exclusive).

console.log(Math.random());	//sample value 0.7186374642629878

There are some other methods as well for trigonometry.

  • sin() to get sine of the number passed as parameter
  • cos() to get cosine of the number passed as parameter
  • tan() to get tangent of the number passed as parameter
  • asin() to get arc sine of the number passed as parameter
  • acos() to get arc cosine of the number passed as parameter
  • atan() to get arc tanget of the number passed as parameter
Share

Recently Added