5 important method about javascript Number
Javascript has only one type of number like decimal and without decimal.
- isFinite()
It identifies if a given number is finite or not. It doesn’t change the type of number just check the finiteness of the number and return true or false.
const number1 = 1.2;console.log(Number.isFinite(number1)); // trueconst number2 = ‘infinity’;console.log(Number.isFinite(number2)); // false
2. isInteger()
it checks if a given number is an integer or not. If the number is an integer it passes true and if it is infinity, decimal, it will pass false.
const number1 = 3;console.log(Number.isInteger(number1)); // trueconst number2 = 1.2;console.log(Number.isInteger(number2)); // false
3. isNaN()
NaN means Not a Number. isNaN method determines if a given value is not a number or not. If a value is not a number then it will pass true otherwise it will pass false.
const number1 = 0/0;console.log(Number.isNaN(number1)); // trueconst number2 = 1;console.log(Number.isNaN(number2)); // false
4. toFixed()
If you want to convert a number to a string with a rounding operation, then you can use this method.
const number1 = 4.567;console.log(number1.toFixed(2)); // 4.57
5. toString()
This method converts a number to a string.
const number1 = 4.567;console.log(number1.toString()); // 4.57console.log(typeof number1.toString()); // string