
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to check whether a value is a safe integer or not in JavaScript?
In this tutorial, we will learn to check whether a value is a safe integer or not in JavaScript. The simple definition of the safe integer in JavaScript is all numbers we can represent under the IEEE-754 double-precision number. It is the set of all numbers which lie between the -(2^53) to (2^53) exclusive, and we can represent it in the standard way.
Here, we have different approaches to checking whether the number is a safe integer.
Using the Number.IsSafeInteger() Method
Using the if-else Conditional Statement
Using the Number.isSafeInteger() Method
In JavaScript, the isSafeInteger() method checks that type of value is a number and is between the -(2^53) to (2^53). We can pass the different values as a parameter of the method, and it returns the Boolean value as a result. If the number is a safe integer, it returns true otherwise false.
Syntax
Users can use the syntax below to use the isSafeInteger() method
let isSafe = Number.isSafeInteger(value);
Parameters
value − It is the value of any variable for which user wants to check whether value is safe integer or not.
Example
In the below example, we have used the Number.isSafeInteger() method to check whether the value is a safe integer. We checked it for different values such as Boolean, string, float, and integer
<html> <head> </head> <body> <h2>Check if value is safe Integer or not in JavaScript.</h2> <h4>Check if value is safe Integer or not using <i> isSafeInteger() </i> method.</h4> <div id = "output"></div> <script> var output = document.getElementById("output"); let value = 1000; let isSafe = Number.isSafeInteger(value); output.innerHTML += "1000 is safe Integer : " + isSafe + " <br/> "; output.innerHTML += "true is safe Integer : " + Number.isSafeInteger(true) + " <br/> "; output.innerHTML += "'Hello' is safe Integer : " + Number.isSafeInteger("hello") + " <br/> "; output.innerHTML += "Math.pow(2,53) is safe Integer : " + Number.isSafeInteger(Math.pow(2, 53)) + " <br/> "; output.innerHTML += "123.43 is safe Integer : " + Number.isSafeInteger(123.43) + " <br/> "; output.innerHTML += "-90 is safe Integer : " + Number.isSafeInteger(-90) + " <br/> "; </script> </body> </html>
Using the if-else Conditional Statement
In this method, we will simply use the if-else statement to check whether the value is a type of number or not. If the value is a number, we will check if it is between the -(2^53) to (2^53). It is the custom logic based on that isSafeInteger() method that returns the Boolean output.
Syntax
The syntax to check whether values is safe integer or not using the if-else statement is given below.
if ( typeof value === 'number' && -Math.pow(2, 53) < value && Math.pow(2, 53) > value ) { // value is safe integer } else { // value is not safe integer }
Example
In the example below, we have simply implemented the above method. We have created the function named safeInteger(), which checks for the safe integer according to the method explained above and returns the Boolean value.
<html> <head> </head> <body> <h2>Check if value is safe Integer or not in JavaScript.</h2> <h4>Check if value is safe Integer or not using <i> if-else</i> statement.</h4> <div id = "output"></div> <script> var output = document.getElementById("output"); function safeInteger(value) { if ( typeof value === 'number' && -Math.pow(2, 53) < value &&Math.pow(2, 53) > value ) { return true; } else { return false; } } output.innerHTML += " safeInteger (-90) : " + safeInteger(-90) + " <br/> "; output.innerHTML += " safeInteger (true) : " + safeInteger(true) + " <br/> "; output.innerHTML += " safeInteger ('yes') : " + safeInteger('yes') + " <br/> "; output.innerHTML += " safeInteger (123.6543) : " + safeInteger(123.6543) + " <br/> "; </script> </body> </html>
We have learned two different approaches to checking whether the value is a safe integer is not. In the first approach, we used the built-in method; in the second approach, we implemented the first method from scratch.
- Related Articles
- How to find whether a provided number is safe integer or not in JavaScript?
- JavaScript: How to check whether an array includes a particular value or not?
- How to check whether a NaN is a NaN or not in JavaScript?
- How to check whether a number is finite or not in JavaScript?
- How to convert a value to a safe integer using JavaScript?
- How to check whether a key exist in JavaScript object or not?
- Check whether a number is a Fibonacci number or not JavaScript
- How to check whether a value is a number in JavaScript?
- Check whether the entered value is a digit or not in Java
- Check whether the entered value is a letter or not in Java
- How to check whether or not a browser tab is currently active using JavaScript?
- How to check whether a vector contains an NA value or not in R?
- How to check whether a number is a prime number or not?
- How to check whether a thread is alive or not in C#
- How to check whether a thread is a background thread or not in C#
