Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Convert a String to an Integer in JavaScript
Next article icon

How to convert string of any base to an integer in JavaScript ?

Last Updated : 12 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Next Article
Convert a String to an Integer in JavaScript
author
amansingla
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

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. [GFGTABS] JavaScript let s = "100.45"; console.log(typeof Number(s)); [/GFGTABS]O
    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 s
    2 min read
  • How to convert Integer array to String array using JavaScript ?
    The task is to convert an integer array to a string array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. Approaches to convert Integer array to String array:Table of Content Approach 1: using JavaScript array.map() and toString() methodsApproach 2: U
    2 min read
  • How to convert an Integer Into a String in PHP ?
    The PHP strval() function is used to convert an Integer Into a String in PHP. There are many other methods to convert an integer into a string. In this article, we will learn many methods. Table of Content Using strval() function.Using Inline variable parsing.Using Explicit Casting.Using sprintf() F
    3 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 image into base64 string using JavaScript ?
    In this article, we will convert an image into a base64 string using Javascript. The below approaches show the methods to convert an image into a base64 string using Javascript. ApproachHere we will create a gfg.js file which will include JavaScript code and one gfg.html file.Now we will put onchang
    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 Content Using JavaScript methodsUsing Custom functionUsing logarithmsUs
    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
  • How to Iterate Over Characters of a String in JavaScript ?
    There are several methods to iterate over characters of a string in JavaScript. 1. Using for LoopThe classic for loop is one of the most common ways to iterate over a string. Here, we loop through the string by indexing each character based on the string's length. Syntax for (statement 1 ; statement
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences