Functions that determine the current state of the Number object. Functions isFinite(), isInteger(), isNan(), isSafeInteger()
Contents
- 1. Functions that determine the state of the Number object. List
- 2. Function isFinite(). Determining the sign of the finiteness of a number
- 3. Function isInteger(). Determining the sign of an integer
- 4. Function isNan(). Determine if the specified value is non-numeric
- 5. Function isSafeInteger(). Determine if a value is a safe integer
- Related topics
Search other resources:
1. Functions that determine the state of the Number object. List
In the Java language, all numbers are represented by the Number type. In other words, for each number, a Number object is defined. When writing programs, there are cases when this number may have an unacceptable state for use, for example, infinity when divided by 0. In this case, you need to perform the necessary verification. To handle the various states of a number, there are 4 functions of the Number object that are useful in JavaScript:
- isFinite() – determines if the number is finite;
- isInteger() – determines whether the number is an integer;
- isNan() – determines if the value is not a number;
- isSafeInteger() – determines whether an integer value is safe according to the IEEE-754 standard.
⇑
2. Function isFinite(). Determining the sign of the finiteness of a number
The isFinite() function determines if the input number is finite. You can call the function in the following ways
isFinite(value)
or
Number.isFinite(value)
here
- value – the value being tested for finiteness.
The function returns a boolean value. If the number is finite, then the function returns true. Otherwise (the number is not finite) the function returns false.
Example.
// The Number type // Function isFinite() var a = 10 var res = Number.isFinite(a) // res = true res = isFinite(a / 0) // res = false var b = a / 0 res = isFinite(b) // res = false
⇑
3. Function isInteger(). Determining the sign of an integer
The isInteger() function allows you to determine whether the specified number is an integer. The general function call could be as follows:
Number.isInteger(value)
here
- value – value to be checked.
The function returns true if the number is an integer. If the number contains a fractional part, the function returns false.
Example.
// Function isInteger() - determine if a number is an integer // 1. Get the result for a number var a = 10 var res = Number.isInteger(a) // res = true // 2. Get the result of an expression res = Number.isInteger(2+3.5) // res = false res = Number.isInteger(20/4) // res = true alert(res) // 3. Using a function in a conditional statement var x = 3 / 8 if (Number.isInteger(x)) // false alert("isInteger(3/8) => true"); else alert("isInteger(3/8) => false");
⇑
4. Function isNan(). Determine if the specified value is non-numeric
The isNan() function can be used to determine if a value is not numeric. In other words, the function determines whether a number can be formed from some value. In the most general case, a function call has the form
Number.isNan(value)
or
isNan(value)
here
- value – the value to check for a sign of a number.
The function returns true if value cannot be converted to a number. Otherwise, the function returns false.
Example.
// Function isNan() - determine if value is non-numeric var res = Number.isNaN("10") // res = false var a = 10 res = Number.isNaN(a) // res = false res = Number.isNaN("abcd") // res = false alert(res) res = Number.isNaN(a/0) // res = false res = Number.isNaN(0/0) // res = true res = isNaN("5"/"6") // res = false res = isNaN("abcd"/0) // res = true res = isNaN(NaN) // res = true alert(res) // Using the function in a conditional statement var x = 0 / 0 if (Number.isNaN(x)) // true alert("isNaN(0/0) => true"); else alert("isNaN(0/0) => false");
⇑
5. Function isSafeInteger(). Determine if a value is a safe integer
The isSafeInteger() function determines if the given integer is safe.
An integer is considered safe if it:
- can be represented as a double precision number according to the IEEE-754 standard;
- in the representation of the IEEE-754 standard, cannot be the result of rounding off any other integer in order to comply with this standard.
The IEEE-754 standard describes a format for representing floating point numbers.
The following constants are used to define the safety limits of an integer:
Number.MIN_SAFE_INTEGER Number.MAX_SAFE_INTEGER
These constants define the upper and lower bounds of an integer, respectively.
The MIN_SAFE_INTEGER and MAX_SAFE_INTEGER constants have the following values
MIN_SAFE_INTEGER = -(253 - 1) MAX_SAFE_INTEGER = 253 - 1
A function call can be made using the name of the Number object:
Number.isSafeInteger(value)
here
- value is the value (or expression) being checked for security.
Example.
// Function isSafeInteger() - determine if an integer is safe var res = Number.isSafeInteger(150) // res = true res = Number.isSafeInteger(Number.MAX_SAFE_INTEGER+1) // res = false res = Number.isSafeInteger(Number.MIN_SAFE_INTEGER-1) // res = false res = Number.isSafeInteger(Math.pow(2, 53)) // res = false res = Number.isSafeInteger(Math.pow(2, 53) - 1) // res = true res = Number.isSafeInteger(-Math.pow(2, 53)) // res = false res = Number.isSafeInteger(-Math.pow(2, 53) + 1) // res = true
⇑
Related topics
- Numeric data type Number. Conversion functions between numeric type and other types. Functions Number(), parseInt(), parseFloat()
- Functions for representing the Number object in different notations. Functions toExponential(), toFixed(), toLocaleString(), toPrecision(), toString(), valueOf()
- Properties of Number object
⇑