What are the Number Methods used in ES6 ?
Last Updated :
18 Oct, 2021
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));
console.log(Number.isNaN(0/0));
console.log(Number.isNaN(10));
console.log(Number.isNaN( "Sarthak" ));
console.log(Number.isNaN( "NaN" ));
</script>
|
Output:
Number.isNaN()
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))
console.log(Number.isInteger(-9999))
console.log(Number.isInteger(99999))
console.log(Number.isInteger( "Sarthak Delori" ))
console.log(Number.isInteger(undefined))
</script>
|
Output:
Number.isInteger()
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))
console.log(Number.isSafeInteger(Math.pow(2,53)-1))
console.log(Number.isSafeInteger(Math.pow(2,53)))
console.log(Number.isSafeInteger( "Sarthak Delori" ))
console.log(Number.isSafeInteger(undefined))
</script>
|
Output:
Number.isSafeInteger
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))
console.log(Number.isFinite(2e64))
console.log(Number.isFinite( null ))
console.log(Number.isFinite( "Sarthak Delori" ))
console.log(Number.isFinite(undefined))
console.log( "For global isFinite():" )
console.log(window.isFinite( null ))
console.log(window.isFinite( "999" ))
console.log(window.isFinite( "Sarthak" ))
</script>
|
Output:
Number.isFinite
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))
console.log(Number.parseFloat(45.1))
console.log(Number.parseFloat( "45.87" ))
console.log(Number.parseFloat( "45.87SarthakDelori" ))
console.log(Number.parseFloat( "Sarthak45.87" ))
console.log(Number.parseFloat( "Sarthak Delori" ))
console.log( "For parseInt():" )
console.log(window.parseInt(45))
console.log(window.parseInt(45.1))
console.log(window.parseInt( "45.87Sarthak" ))
console.log(window.parseInt( "Sarthak45" ))
console.log(Number.parseInt( "Sarthak Delori" ))
</script>
|
Output:
parseFloat and parseInt
Share your thoughts in the comments
Please Login to comment...