Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
communitycreator

How to check if a value is a safe integer using Lodash

Shubham Singh Kshatriya

Overview

An integer is a safe integer if:

  • It can be represented as an IEEE-754 double-precision number.
  • It is not a result of any integer rounded to be represented as an IEEE-754 double-precision number.

Note: Any integer that falls in the range [-(2^53 - 1), 2^53 - 1] is a safe integer.

We can use the _.isSafeInteger() method in Lodash to check if a value is a safe integer.

Syntax

The syntax to check the safe integer is:

_.isSafeInteger(value)
Syntax of the _.isSafeInteger() method

Parameters

This method accepts the following parameters:

  • value: This is the value to be checked.

Return value

This method returns true if the value is a safe integer. Otherwise, it returns false.

Coding example

Let’s look at an example of the _.isSafeInteger() method in the code snippet below:

Console
Implementation of the _.isSafeInteger() method

Code explanation

In the HTML tab:

  • Line 5: We import the lodash library.

In the JavaScript tab:

  • Lines 2 to 9: We create a function isValueASafeInteger() that accepts a value and checks if it is a safe integer using the _.isSafeInteger() method, and print the result on the console.
  • Line 12 to 16: We create a few variables and initialize them.
  • Line 19 to 23: We invoke the isValueASafeInteger() function for each of the variables declared.

RELATED TAGS

lodash
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya
RELATED COURSES

View all Courses

Keep Exploring