
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to convert a string of any base to an integer in JavaScript?
An integer can be represented in various formats as described in the computed languages i.e. Binary, Decimal, Hexadecimal, etc. A Binary number consists of two digits only i.e. 0 & 1, whereas a decimal consists of digits from 0 to 9.
We can convert a string to an integer by using the parseInt() conversion by default. But in other cases when the integer is represented in some other form we also need to pass the base string to decode it into the resultant number.
Below is the syntax of parsing a string to an int.
Syntax
1. When the base value is provided
parseInt(string_value, base)
2. When the base value is not provided
parseInt(string_value)
By default, the parseInt() method converts into a decimal value.
Example 1
In the below example, we are converting a string value into an Integer by using the default method and without passing the base value.
# index.html
<!DOCTYPE html> <html> <head> <title> Creating Objects from Prototype </title> </head> <body> <h2 style="color:green"> Welcome To Tutorials Point </h2> </body> <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("8475628"); stringConversion("101010"); stringConversion("0x3278"); </script> </html>
Output
On successful execution of the above program, the browser will display the following result −
Welcome To Tutorials Point
And in the console, you will find all the results, see the screenshot below −
Example 2
In the below example, we are converting a string value into an Integer by also passing the base value along with the string.
# index.html
<!DOCTYPE html> <html> <head> <title> Creating Objects from Prototype </title> </head> <body> <h2 style="color:green"> Welcome To Tutorials Point </h2> </body> <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("101010", 2); StringConversion("101", 10); StringConversion("256472", 8); </script> </html>
Output
On successful execution of the above program, the browser will display the following result −
Welcome To Tutorials Point
And in the console, you will find all the results, see the screenshot below −
- Related Articles
- Convert the string of any base to integer in JavaScript
- How to convert a string into integer in JavaScript?
- How to convert an integer to a hexadecimal string in Python?
- How to convert an integer to a octal string in Python?
- How to convert a string into an integer without using parseInt() function in JavaScript?
- How do I convert a string into an integer in JavaScript?
- How to convert String to Integer and Integer to String in Java?
- Convert an integer to a hex string in C++
- Convert integer array to string array in JavaScript?
- How to convert a string to a integer in C
- How to convert a string vector into an integer vector in R?
- How to convert a Boolean to Integer in JavaScript?
- How to convert an integer to string with padding zero in C#?
- How do I convert an integer to binary in JavaScript?
- How to convert an image to Base 64 string on Android?
