
- 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 into integer in JavaScript?
In this article, we’ll go over how to convert a string into integer in JavaScript with appropriate examples.
To convert a string to an integer parseInt(), Number(), and Unary operator(+) 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.
To convert a string into integer we can use parseInt(), Number() and Unary operator(+). Each of them is discussed with an example.
Using the parseInt() method
The syntax for parseInt() method is −
parseInt(string); parseInt(string,radix);
Where,
string is the string to be parsed.
radix is used to mention the base in Number System. 2 for binary, 8 for octal, 10 for decimal and 16 for hexadecimal.
This method returns the integer value that is parsed from the string.
Example
This is an example program to convert a string into an integer using parseInt() method.
<!DOCTYPE html> <html> <head> <title>To convert a string to an integer in JavaScript</title> </head> <body style="text-align : center"> <h3></h3> <p id='strToInt'>To convert a string to an integer in JavaScript</p> <script> //ParseInt() parses a string and returns a whole number, else NaN. let a = parseInt("100"); let b = parseInt("100.234"); let c = parseInt([1.2]); let d = parseInt(true); let e = parseInt("hello") document.getElementById('strToInt').innerHTML = `parseInt("100") is ${a} ${'<br/>'} parseInt("100.234") is ${b} ${'<br/>'} parseInt([1.2]) is ${c} ${'<br/>'} parseInt(true) is ${d} ${'<br/>'} parseInt('hello') is ${e} `; </script> </body> </html>
Using the Number() method
The syntax for Number() method is
Number(value)
Where, value can be of any primitive data type that is convertible to number, else it returns NaN. This method returns a number.
Example
This is an example program to convert a string into an integer using Number() method.
<!DOCTYPE html> <html> <head> <title>To convert a string into integer in JavaScript</title> </head> <body style="text-align : center"> <h3></h3> <p id='valToNum'>To convert a string into integer in JavaScript using Number()</p> <script> let a = Number("100"); let b = parseInt(Number("100.234")); let c = Number(true); //return 0 or 1 for boolean let d = Number("true"); //returns a number or NaN let e = Number(new Date()); //returns number of milli seconds from Jan 1 1970 let f = Number([1]); document.getElementById('valToNum').innerHTML = `Number("100") is ${a} ${'<br/>'} Number("100.234") is ${b} ${'<br/>'} Number(true) is ${c} ${'<br/>'} Number("true") is ${d} ${'<br/>'} Number(new Date()) is ${e} ${'<br/>'} Number([1]) is ${f} `; </script> </body> </html>
On executing the above code, the following output is generated.
Using the unary operator
The syntax for unary plus operator is −
+op;
Where, op is an operand. The unary operator + is placed before the operand and tries to convert it into a number. A number is returned.
Example
This is an example program to convert a string into an integer using Unary operator +.
<!DOCTYPE html> <html> <head> <title>To convert a string into Integer in JavaScript</title> </head> <body style="text-align : center"> <h3>To convert a string into integer in JavaScript using Unary operator</h3> <p id='valToNum'></p> <script> // Unary operator converts a string to a Number. let a = +"100"; let b = +"-100.234"; document.getElementById('valToNum').innerHTML = `+"100" is ${a} ${'<br/>'} +"-100.234" is ${b} ${'<br/>'} `; </script> </body> </html>
On executing the above code, the following output is generated.
- Related Articles
- How do I convert a string into an integer in JavaScript?
- How to convert a string into an integer without using parseInt() function in JavaScript?
- How to convert a string vector into an integer vector in R?
- How to convert string into float in JavaScript?
- Convert integer array to string array in JavaScript?
- How to convert a JSON string into a JavaScript object?
- How to convert a string of any base to an integer in JavaScript?
- Haskell Program to convert the string into an integer
- C++ Program to convert the string into an integer
- How to convert String to Integer and Integer to String in Java?
- How to convert an array into JavaScript string?
- How to convert a string into upper case using JavaScript?
- How to convert a string to a integer in C
- How to convert a Boolean to Integer in JavaScript?
- How to convert the image into a base64 string using JavaScript?
