Open In App

JavaScript Program for String to Long Conversion

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, there is no specific data type called “Long”, but it uses the “Number” to represent the integer values. We can indirectly convert the String to Long by converting them to Number.

There are several approaches in Javascript to convert strings to long which are as follows:

Using parseInt

In JavaScript parseInt is a function used to convert a string to an integer. It takes a string argument, like “123“, and returns the integer representation, which ignores leading whitespaces and stops at the first non-numeric character.

Syntax:

parseInt(string, radix);

Example: The below example uses parseInt to perform String to Long Conversion.

Javascript




let str = "12345678901234567890";
let res = parseInt(str);
console.log(res);


Output

12345678901234567000

Using Number

Number in JavaScript is a built-in function that converts a value to a number. It can be used to parse strings into numeric values, including integers and floating-point numbers.

Syntax:

Number(value); 

Example: The below example uses Number to perform String to Long Conversion.

Javascript




let str = "12345678901234567890";
let res = Number(str);
console.log(res);


Output

12345678901234567000

Using parseFloat

The parseFloat in JavaScript is a function used to convert a string to a floating-point number. It parses the input string, extracting and returning the numeric portion.

Syntax:

parseFloat(string); 

Example: The below example uses parseFloat to perform String to Long Conversion.

Javascript




let str = "12345678901234567890";
let res = parseFloat(str);
console.log(res);


Output

12345678901234567000

Using BigInt

The BigInt in JavaScript is a built-in object and constructor that allows representation of integers with arbitrary precision. It is used for handling large integer values that exceed the safe integer limit of the standard Number type.

Syntax:

BigInt(value); 

Example: The below example uses BigInt to perform String to Long Conversion.

Javascript




let str = "12345678901234567890";
let res = BigInt(str);
console.log(res);


Output

12345678901234567890n


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads