Js Math object

 

Math

The JavaScript Math object is a built-in object that provides a variety of mathematical functions and constants. It can be used to perform mathematical operations such as trigonometry, logarithms, and random number generation.


One of the most commonly used functions in the Math object is the Math.random() function, which generates a random number between 0 (inclusive) and 1 (exclusive). 


This function can be used to generate random numbers for various purposes such as games, simulations, and other applications.


console.log(Math.random());
// Output: a random number between 0 and 1 (e.g. 0.3456)


You can also use the Math.floor() function to round a number down to the nearest integer.


console.log(Math.floor(3.8));
// Output: 3


The Math.ceil() function rounds a number up to the nearest integer.


console.log(Math.ceil(3.2));
// Output: 4


Another useful function of Math object is Math.max() and Math.min() which are used to find the maximum and minimum values in a set of numbers respectively.


console.log(Math.max(3,5,7,9));
// Output: 9
console.log(Math.min(3,5,7,9));
// Output: 3


Math object also provides the most commonly used mathematical constants such as Math.PI for the value of pi, Math.E for the value of the mathematical constant e.


console.log(Math.PI);
// Output: 3.141592653589793
console.log(Math.E);
// Output: 2.718281828459045


In this way, you can use the Math object in JavaScript to perform mathematical operations, generate random numbers, and access mathematical constants. It is a powerful tool that can be used to add more functionality to your JavaScript programs and make them more interactive and dynamic.