Numbers are one of the primitive data types in JavaScript. JavaScript does not have any specific data type (like int, float, long, etc) for different numbers. It has only one data type i.e. Number. It can be written with or without decimals. They are basically a 64-bit double-precision floating-point value.
There are 6 different Number methods that are used in ES6:
1. Number.isNaN(): It is used to check whether the passed value is NaN or not. If it is a number or string or undefined, it will give false.
Example:
Javascript
<script> console.log(Number.isNaN(NaN)); // true console.log(Number.isNaN(0/0)); // true console.log(Number.isNaN(10)); // false console.log(Number.isNaN( "Sarthak" )); // false console.log(Number.isNaN( "NaN" )); // false </script> |
Output:
2. Number.isInteger(): It is used to determine whether the value passed is a integer or not. The number has to be only integer (negative or positive), else the method will give false.
Example:
Javascript
<script> console.log(Number.isInteger(0)) // true console.log(Number.isInteger(-9999)) // true console.log(Number.isInteger(99999)) // true console.log(Number.isInteger( "Sarthak Delori" )) // false console.log(Number.isInteger(undefined)) // false </script> |
Output:
3. Number.isSafeInteger(): It Is quite similar to Number.isInteger() in working, as it also determines whether the value passed is integer or not but within the range of -(2^53 – 1) to (2^53 – 1).
Example:
Javascript
<script> console.log(Number.isSafeInteger(0)) // true console.log(Number.isSafeInteger(Math.pow(2,53)-1)) // true console.log(Number.isSafeInteger(Math.pow(2,53))) // false console.log(Number.isSafeInteger( "Sarthak Delori" )) // false console.log(Number.isSafeInteger(undefined)) // false </script> |
Output:
4. Number.isFinite(): It determines whether the passed value is finite or not. It basically takes a number and check if it is a 64-bit number. If the passed value is a string or null value, it gives. But JavaScript has a global isFinite() also which gives true in all the cases whether it is a number of less than or equal to 64 bits or a string (of a number like “999” and not “Sarthak”) or even a null value.
Example:
Javascript
<script> console.log( "For Number.isFinite():" ) console.log(Number.isFinite(0)) // true // 2e64 === 2*10^64 console.log(Number.isFinite(2e64)) // true console.log(Number.isFinite( null )) // false console.log(Number.isFinite( "Sarthak Delori" )) // false console.log(Number.isFinite(undefined)) // false console.log( "For global isFinite():" ) console.log(window.isFinite( null )) // true console.log(window.isFinite( "999" )) // true console.log(window.isFinite( "Sarthak" )) // false </script> |
Output:
5. Number.parseFloat() and Number.parseInt(): These two are the most important number methods and they work similarly to their global functions. They parse the passed value to their respective types. parseFloat parses to a float value and parseInt parses to an int value.
Example:
Javascript
<script> console.log( "For parseFloat():" ) console.log(Number.parseFloat(45)) // 45 console.log(Number.parseFloat(45.1)) // 45.1 console.log(Number.parseFloat( "45.87" )) // 45.87 console.log(Number.parseFloat( "45.87SarthakDelori" )) // 45.87 console.log(Number.parseFloat( "Sarthak45.87" )) // NaN console.log(Number.parseFloat( "Sarthak Delori" )) // NaN console.log( "For parseInt():" ) console.log(window.parseInt(45)) // 45 console.log(window.parseInt(45.1)) // 45 console.log(window.parseInt( "45.87Sarthak" )) // 45 console.log(window.parseInt( "Sarthak45" )) // NaN console.log(Number.parseInt( "Sarthak Delori" )) // NaN </script> |
Output: