Horje
JavaScript Program for String to Long Conversion

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

Using Unary Plus (+)

The Unary Plus (+) operator can be used to convert a string to a number. It is a simple and concise way to perform the conversion. However, it is important to note that this method works well for numbers that are within the safe integer range.

Example: The below example uses the Unary Plus operator to perform String to Long Conversion.

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

Output
12345678901234567000



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Print Floyd’s Pattern Triangle Pyramid JavaScript Program to Print Floyd’s Pattern Triangle Pyramid
How to Scroll to Bottom of Div in JavaScript ? How to Scroll to Bottom of Div in JavaScript ?
JavaScript Program to Print all Even Numbers in a Sorted Order of a Linked List JavaScript Program to Print all Even Numbers in a Sorted Order of a Linked List
JavaScript Program to Reverse a Stack using Recursion JavaScript Program to Reverse a Stack using Recursion
How to Check if an Array Includes an Object in TypeScript ? How to Check if an Array Includes an Object in TypeScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15