

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a string into integer in JavaScript?
To convert a string to an integer parseInt() function is used in javascript. parseInt() function returns Nan( not a number) when the string doesn’t contain number. If a string with a number is sent then only that number will be returned as the output. This function won't accept spaces. If any particular number with spaces is sent then the part of the number that presents before space will be returned as the output.
syntax
parseInt(value);
This function takes a string and converts it into an integer. If there is no integer present in the string, NaN will be the output.
Example
In the following example, various cases of strings such as only strings, strings with numbers, etc have been taken and sent through the parseInt() function. Later on, their integer values, if present, have displayed as shown in the output.
<html> <body> <script> var a = "10"; var b = parseInt(a); document.write("value is " + b); var c = parseInt("423-0-567"); document.write("</br>"); document.write('value is ' + c); document.write("</br>"); var d = "string"; var e = parseInt(d); document.write("value is " + e); document.write("</br>"); var f = parseInt("2string"); document.write("value is " + f); </script> </body> </html>
Output
value is 10 value is 423 value is NaN value is 2
- Related Questions & Answers
- How do I convert a string into an integer in JavaScript?
- How to convert Python DateTime string into integer milliseconds?
- How to convert a string vector into an integer vector in R?
- How to convert a JSON string into a JavaScript object?
- Convert integer array to string array in JavaScript?
- How to convert an array into JavaScript string?
- How to convert String to Integer and Integer to String in Java?
- How to convert a string into upper case using JavaScript?
- How to convert a string to a integer in C
- How to convert a string of any base to an integer in JavaScript?
- How to convert an integer into a date object in Python?
- How to convert a Boolean to Integer in JavaScript?
- How to convert the image into a base64 string using JavaScript?
- How to convert a string into the lower case using JavaScript?
- Convert list of string into sorted list of integer in Python