How to convert string of any base to an integer in JavaScript ?
Last Updated :
12 Jan, 2022
Given a string containing an integer value and along with that user passes a base value. We need to convert that string of any base value to an integer in JavaScript.
String Integer
"1002" 1002
For performing the above-illustrated task, we would be using a method (or a function) provided by JavaScript called as parseInt().
This is a special method, provided by JavaScript, that takes an integer value (of any base which is either specified or not) and further converts the string into an integer value.
Syntax:
Following is the syntax that a user may use to convert a string into an integer value (of any base)-
parseInt(string_value, base)
Alternatively, if we don't want to specify the base value and just want to convert our string value into an integer value itself, then we may use the following syntax also-
parseInt(string_value)
Default value returned by base or radix of parseInt() method is 10. In other words, if we don't specify any base or radix value then it by default converts the string value to an integer value by taking into regard the base or radix value as 10.
Let us visualize all of the above-illustrated facts with some of the following examples-
Example 1: In this example, we would be passing the string value in a method (which is explicitly declared for ease purpose) and further that string value is passed inside the parseInt() method which then further converts that string value in the corresponding integer value.
JavaScript
<script>
let stringConversion = (string_value) => {
console.log("Initial Type: " + typeof string_value);
let integer_value = parseInt(string_value);
console.log("Final Type: " + typeof integer_value);
console.log(integer_value);
};
stringConversion("512000");
stringConversion("126410");
stringConversion("0x8975");
</script>
Output:
Initial Type: string
Final Type: number
512000
Initial Type: string
Final Type: number
126410
Initial Type: string
Final Type: number
35189
Example 2: In this example, we would be passing the base value explicitly inside the method which is then further taken by the parseInt() method and further converts the string value into the corresponding integer value by taking into account the specified base value also.
JavaScript
<script>
let StringConversion = (string_value, base) => {
console.log(
"String value is : " + string_value +
" and base value is: " + base
);
let Integer_value = parseInt(string_value, base);
console.log("Integer value is: " + Integer_value);
};
StringConversion("1011", 2);
StringConversion("101", 10);
StringConversion("10002", 8);
</script>
Output:
String value is : 1011 and base value is: 2
Integer value is: 11
String value is : 101 and base value is: 10
Integer value is: 101
String value is : 10002 and base value is: 8
Integer value is: 4098
Similar Reads
Convert a String to an Integer in JavaScript These are the following ways to convert string to an integer in JavaScript:1. Using Number() MethodThe number() method converts a string into an integer number. It works similarly to the unary plus operator. JavaScriptlet s = "100.45"; console.log(typeof Number(s));Outputnumber 2. Using Unary + Oper
2 min read
How To Convert Base64 to JSON String in JavaScript? There could be situations in web applications, where there is a need to decode the data from Base64 format back into its original JSON format. It generally happens when one has to transmit data over the network where Base64 encoding is well suited for encoding binary data.In this article, we will se
2 min read
How to convert array of strings to array of numbers in JavaScript ? Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati
4 min read
How to Convert JSON to base64 in JavaScript ? Base 64 is the encoding scheme that represents binary data in a printable ASCII format, commonly used for data serialization and transmission. Table of Content Using btoa functionUsing Manual ConversionUsing btoa functionIn this approach, we're using btoa to encode a UTF-8 string representation of a
2 min read
How to convert long number into abbreviated string in JavaScript ? We are given a long number and the task is to convert it to the abbreviated string(eg.. 1234 to 1.2k). Here 2 approaches are discussed with the help of JavaScript.Approaches to Convert Long Number to Abbreviated String:Table of ContentUsing JavaScript methodsUsing Custom functionUsing logarithmsUsin
6 min read
How to Get the Length of a String in Bytes in JavaScript ? In JavaScript, determining the length of a string in characters is straightforward using the length property. However, in many cases, particularly when dealing with file sizes, network protocols, or database storage, you might need to know the length of a string in bytes. This is because characters
3 min read