JavaScript Number & Math
Computers work by processing and manipulating numbers. Most programming languages make a distinction between whole numbers and decimal numbers. In JavaScript, the number type is a double-precision 64-bit format IEEE 754 value. According to this, There is no such thing as an integer in JavaScript (except BigInt). A number say 13 in JavaScript is a floating-point value, not an integer. Let’s have a look at some basics functions of the number to work with.
isNaN():
NaN means Not A Number. So isNaN() function implicitly checks whether a value is not-a-number or not. If the value is not a number (NaN) then this function returns but if the value becomes a legal number then it returns the false.
isNaN(789); //false
isNaN(-3.14); //false
isNaN(7 / 3); //false
isNaN(0); //false
isNaN('123'); //false
isNaN('Hello'); //true
isNaN(NaN); //true
isNaN(0 / 0); //true
isNaN(true); //false
parseFloat() & parseInt():
parseFloat() function parses a string and returns a floating point number. As like as parseInt() function parses a string and returns an integer number. The difference between parseInt() & parseFloat() is parseInt() accepts second argument. It’s called radix. It represents numeral system to be used but parseFloat() only accepts one argument. By using parseInt() we can convert number into binary or hexadecimal code.
parseInt("1234blue"); //returns 1234
parseFloat("1234blue"); //returns 1234parseInt("0xA"); //returns 10
parseFloat("0xA"); //returns 0parseInt("22.5"); //returns 22
parseFloat("22.5"); //returns 22.5parseInt("blue"); //returns NaN
parseFloat("blue"); //returns NaNparseInt("22.34.5"); //returns 22
parseFloat("22.34.5"); // returns 22.34parseInt("10", 2); //returns 2
parseInt("10", 8); //returns 8
parseInt("10", 10); //returns 10
parseInt("10", 16); //returns 16parseInt("40 years"); //returns 40
parseFloat(" 60 "); //returns 60
Some Math Functions:
Math.abs():
Math.abs() method returns the absolute value of a number.
Math.abs(-5.94); // 5.94
Math.abs(5.94); // 5.94
Math.abs(null); // 0
Math.ceil() & Math.floor():
Math.ceil() method rounds a number UPWARDS to the nearest integer and Math.floor() method rounds a number DOWNWARDS to the nearest integer and then return the result.
Math.ceil(1.4); // 2
Math.floor(1.4); // 1
Math.ceil(-1.4); // -1
Math.floor(-1.4); // -2
Math.min() & Math.max():
Both are static function. Math.min() function returns the lowest-valued number and Math.max() function returns the heighest-valued number passed into it.
Math.min(2, 3, 1); // 1
Math.max(2, 3, 1); // 3
const arr = [25, 30, 35, 40]
Math.min(...arr); // 25
Math.max(...arr); // 40
Math.random():
Math.random() function returns a random floating-point number between 0 and 1 (inclusive of 0, but not 1).
Math.random(); // random floating-point number between 0 and 1
Math.floor(Math.random() * 11); // random integer from 0 to 10
Math.floor(Math.random() * 6) + 1; // random integer from 1 to 6
Math.random() * (-2) + 1; //random floating number between -2 and -1
Math.sqrt():
math.sqrt() function returns the square root of a number.
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095
Math.sqrt(0); // 0
Math.sqrt(-2); // NaN
Math.sqrt(-0); // -0
References: