Math is an object in JavaScript that provides mathematical functions and constants. There is no need to create an instance of the object we can directly access the methods of this object by simply calling it with the "." operator e.g. Math.round(x) this will call the round method. The Math object provides properties and methods to perform mathematical operations.
Math methods in js
Math.round(x): The math.round() method returns the value of a number x rounded to the nearest integer.
console.log(Math.round(4.3)); //Output: 4 console.log(Math.round(4.7)); //Output: 5
Math.floor(x): The Math.floor() function in JavaScript is used to round a number down to the nearest integer that is less than or equal to the given number.
console.log(Math.floor(4.3)); //Output: 4 console.log(Math.floor(4.7)); //Output: 4
Math.ceil(x): The Math.ceil() function in JavaScript is used to round a number up to the nearest integer greater than or equal to the given number.
console.log(Math.ceil(4.3)); //Output: 5 console.log(Math.ceil(4.7)); //Output: 5
Math.abs(x): The Math.abs(x) function returns the absolute value of a number x i.e. the non-negative value of x.
console.log(Math.abs(-8)); //Output: 8 -ve values to positive console.log(Math.abs(8)); //Output: 8
Math.min(x,y,...,n): The Math.min() function Returns the smallest value among the given numbers.
console.log(Math.min(4,6,3,9)); //Output: 3
Math.max(x,y,...,n): The Math.max() function Returns the largest value among the given numbers.
console.log(Math.max(4,6,3,9)); //Output: 9
Math.random() and it's use cases: The Math.random() function in JavaScript is used to generate a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). The returned value is a decimal number.
console.log(Math.random()); //Output: 0 random number between 0 to 0.99 //Random number between 0(inclusive to 10 (exclusive) console.log(Math.random()*10); //Output: any random number between 0 to 9 //Random number between 1(inclusive to 10 (inclusive) console.log(Math.floor(Math.random() * 10) + 1); //Output: any random number between 1 to 10 //Get random values between minumun number and maximum number every time const min=10; const max=20; console.log( Math.floor(Math.random()*(max-min+1))+min );
Math.sqrt(x): The Math.sqrt() function in JavaScript is used to calculate the square root of a given number.
let number = 25; let squareRoot = Math.sqrt(number); console.log(squareRoot); // Output: 5
Math.power(x,y): the Math.power() function is used to calculate a base raised to the power of an exponent e.g. to calculate square, cube etc for a number.
let base = 2;
let exponent = 3;
let result = Math.pow(base, exponent);
console.log(result); // Output: 8 (cube i.e. 2 raised to the power of 3)
Different Number methods in js
Number.toFixed(x): The toFixed() method in JavaScript is used to format a number to a specified number of digits after the decimal point and returns a string representation of that number.
let number = 123.456789; let formattedNumber = number.toFixed(2); console.log(formattedNumber); // Output: "123.46" console.log(typeof formattedNumber) //Output: string
Number.toString(x): The Number.toString() method in JavaScript is used to convert a number to a string, optionally specifying the numerical base for the conversion.
const balance = new Number(100) console.log(balance); //specially defined casted as number console.log(balance.toString()); //Output: 100 console.log(typeof balance.toString()); //Output: 100 let number = 10; let binaryString = number.toString(2); console.log(binaryString); // Output: "1010" i.e. in binary number system 10 is represented as 1010 let hexString = number.toString(16); console.log(hexString); // Output: "a" i.e. in hexadecimal number system 10 is represented as a
Number.toString(x).length property: The Number.toString() method converts the given number to string and if we chain the length property with it we will get the length of the converted string representation of a number as shown in example below.
const balance = new Number(100) console.log(balance.toString()); //Output: "100" console.log(balance.toString().length); //Output: 3
Number.toPrecision(x): The Number.toPrecision() method in JavaScript is used to format a number to a specified length, representing the total number of significant digits (both before and after the decimal point).
const otherNumber = 23.866 console.log(otherNumber.toPrecision(3)); //Output: 23.9
Number.toLocaleString(x): The Number.toLocaleString() method in JavaScript is used to format a number into a string using the locale-specific formatting options. It is primarily used for representing numbers in a way that respects the user's locale.
let number = 1234567.89; let formattedNumberUS = number.toLocaleString('en-US'); console.log(formattedNumberUS); // Output: "1,234,567.89" let number = 1234567.89; let formattedNumberIN = number.toLocaleString('en-IN'); console.log(formattedNumberIN); // Output: "12,34,567.89"