I have the following code:

 isBigInt: function () {
        var val = bigInt();
        if (bigInt(val.toString()).greaterOrEquals("-9223372036854775808") && bigInt(val.toString()).lesserOrEquals("9223372036854775807"))
            return true;
        else
            return false;
    }

I have downloaded the biginteger library in the https://www.npmjs.org/package/big-integer , but this code does not get the correct number typed by the user.

Dani AI

Generated

Most likely cause: your code never uses the value the user typed. In the snippet above calls the library constructor with no argument (bigInt()), then converts that result to a string — so you end up comparing a default value instead of the input. The fix is to read the raw input string, validate/clean it, then construct a BigInt (or a big-integer object) from that string before doing range checks.

A concise, robust approach using modern JavaScript BigInt:

function isInSigned64Range(input) {
  const s = String(input).trim().replace(/,/g, '');        // remove commas, whitespace
  if (!/^-?\d+$/.test(s)) return false;                    // reject non-integer text

  try {
    const v = BigInt(s);
    const MIN = -9223372036854775808n;
    const MAX = 9223372036854775807n;
    return v >= MIN && v <= MAX;
  } catch (e) {
    return false; // invalid numeric string for BigInt
  }
}

Notes and troubleshooting

  • If you must use the external big-integer library (instead of native BigInt), pass the user string into its constructor (for example bigInt(userString)) and then compare against big-integer instances — do not call the constructor with no value.
  • Always trim input and strip thousands separators before parsing. Reject decimals (use the regex check) because BigInt only accepts integers.
  • ’s C# example shows good defensive input handling with try/catch; apply the same pattern in JS (validate first, then parse inside try/catch).
  • Remember: BigInt values are a separate type from Number — avoid mixing them in arithmetic without explicit conversion.
static void Main(string[] args)
{
    int numVal = -1;
    bool repeat = true;

    while (repeat == true)
    {
        Console.WriteLine("Enter a number between −2,147,483,648 and +2,147,483,647 (inclusive).");

        string input = Console.ReadLine();

        // ToInt32 can throw FormatException or OverflowException. 
        try
        {
            numVal = Convert.ToInt32(input);
        }
        catch (FormatException e)
        {
            Console.WriteLine("Input string is not a sequence of digits.");
        }
        catch (OverflowException e)
        {
            Console.WriteLine("The number cannot fit in an Int32.");
        }
        finally
        {
            if (numVal < Int32.MaxValue)
            {
                Console.WriteLine("The new value is {0}", numVal + 1);
            }
            else
            {
                Console.WriteLine("numVal cannot be incremented beyond its current value");
            }
        }
        Console.WriteLine("Go again? Y/N");
        string go = Console.ReadLine();
        if (go == "Y" || go == "y")
        {
            repeat = true;
        }
        else
        {
            repeat = false;
        }
    }
    // Keep the console open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.