View all Courses. All rights reserved. Courses Pricing. How to use the parseInt function in JavaScript. Syntax The basic syntax of the function is:. Parameters The parseInt function takes the following parameters as input: String: The string to convert to an Integer value Radix: Specifies the numeral system i. In case it is not mentioned, the following assumptions are made: If the string begins with 0x , the base is considered 16 hexadecimal If the string begins with 0 , the base is considered to be either 8 octal or 10 decimal , depending on the system implementation For any other starting value of the input string, the base is assumed to be 10 decimal Return value If the function parses the string successfully, the integer value is returned.
Keep Exploring. Related Courses. The parseInt method takes two parameters. The first one is the string you want to convert to an integer and is required.
It represents the radix in which your value appears that is required for your conversion. Get matched to a bootcamp today. The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job. The radix argument is not as commonly used as the last one. In most cases, your numbers will be formatted in 10 decimal. The default radix is 10 decimal so you do not need to worry about this argument.
You can include spaces at the start and end of a value that you want to convert to a number. But, spaces in the middle of a number are not allowed. If you try to parse a value that includes letters, you will receive a NaN value.
This is only the case where the letter is relevant to how a number is formatted. We could do so using the following code:. Our program has returned a number instead of a string. We can tell our value is now a number because there are no quotation marks in the value. Our value is a string, which is indicated by the quotation marks surrounding the value.
Then, we use the parseInt method to convert that value to an integer. The JavaScript parseInt method can be used to convert a string to an integer.
This function is useful if you have a string of data that needs to be formatted as a number. No one wants to have to look up a key every time they see one of these values. Unless you have octal or hexadecimal values memorized, or you understand the conversion, the parseInt function can do much of the heavy lifting in converting those values into human readable integers. You might be creating a website component that relies on strictly integer values. The best way to interpret those values would be to use the parseInt function.
Another key practical application of parseInt is for use as a date converter. There will be times where you have to format Date information to make more human-readable text. In a popular example on StackOverflow with over , views a user explains how to use the parseInt function to sanitize date data.
Notice that in most examples parseInt is used to make information more readable. Practical applications of the parseInt function generally make the use of other functions easier.
Knowledge of regular expressions will help you better use of this function. Image Source: Unsplash. The JavaScript parseInt function is usable on all modern web browsers. Basic support is available for all web browser platforms, and each browser supports parsing leading zero strings. This function also has 3 specifications that can easily be read for more technical information on the parseInt function. The following specifications exist for parseInt :.
Another technical case that is important to understand is how different versions of ECMAScript numeric strings with leading zeroes that do not have defined radixes.
ECMAScript3 discourages the use of parsing octals with no radix defined. It is important that you always define your radix when using the parseInt function on octal and hexadecimal values. One of the most common mistakes programmers make when using JavaScript parseInt , is not setting their radix.
A user may parse a string that has a leading zero and receive output that is different from what they are expecting. This Stack Overflow statistic demonstrates that this is a common error with over 14, views over 7 years. To solve this mistake, it is imperative that you understand how parseInt parses strings when no radix is supplied. Always supply a radix, it is better to be explicit when using the parseInt function. Common uses of this function involve parsing strings to convert numbers into an Integer format that can easily be manipulated.
It is important that you remember to define the radix. As always, consult StackOverflow or Quora if you run into bugs that you cannot fix on your own!
Featured Image Source: Pixabay. Filed under: Java by admin No Comments ». Would you like to thank ProgrammerInterview.
0コメント