
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Convert String to Integer in JavaScript
To convert a string into integer in JavaScript, is a simple task and can be converted using various approaches. Converting string to integer is useful for performing accurate mathematical operations, comparisons and validating user input. We will be discussing various approaches in this article to convert a string into integer in JavaScript
In this article, we are having a string value, our task is to convert string to integer in JavaScript.
Approaches to Convert String to Integer in JavaScript
Here is a list of approaches to convert string to integer in JavaScript which we will be discussing in this article with stepwise explaination and complete example codes.
- Conversion to Integer Using parseInt Method
- Conversion to Integer Using bitwise Operator
- Conversion to Integer Using Double Tilde
- Conversion to Integer Using Math.floor Method
- Conversion to Integer Using Math.round Method
- Conversion to Integer Using Math.ceil Method
- Conversion to Integer Using Math.trunc Method
- Conversion to Integer Using BigInt Constructor
Conversion to Integer Using parseInt Method
In this approach we have used parseInt() method to convert string to integer in JavaScript. It takes string as an argument and converts string to integer value.
Example
Here is an example implementing parseInt() method to convert string to integer.
<html> <body> <div id="str"></div> <div id="num"></div> <script> let x=document.getElementById("str"); let str = "123.45"; x.innerHTML = "Input String: " + str + ", It is of type: " +typeof str; let res=document.getElementById("num"); let num = parseInt(str); res.innerHTML = "Result: " + num + ", It is of type: " +typeof num; </script> </body> </html>
Conversion to Integer Using bitwise Operator
In this approach we have used bitwise operator to convert string to integer in JavaScript. We have used bitwise NOT operator which inverts the operand then convert it to 32-bit signed integer converting the string to integer.
Example
Here is an example implementing bitwise NOT operator to convert string to integer.
<html> <body> <div id="str"></div> <div id="num"></div> <script> let x=document.getElementById("str"); let str = "123.12"; x.innerHTML = "Input String: " + str + ", It is of type: " +typeof str; let res=document.getElementById("num"); let num = ~str; res.innerHTML = "Result: " + num + ", It is of type: " +typeof num; </script> </body> </html>
Note: We can also use other bitwise operators such as OR(|), leftshift(<< 0), rightshift(>> 0) and XOR(^) operator to convert a string into integer in javascript.
Conversion to Integer Using Double Tilde
In this approach to convert string to integer in JavaScript, we have used double tilde(~~) operator. It applies NOT operator twice converting string to integer forcing automatic conversion.
Example
Here is an example using double tilde(~~) operator to convert string to integer.
<html> <body> <div id="str"></div> <div id="num"></div> <script> let x=document.getElementById("str"); let str = "123.23"; x.innerHTML = "Input String: " + str + ", It is of type: " +typeof str; let res=document.getElementById("num"); let num = ~~str; res.innerHTML = "Result: " + num + ", It is of type: " +typeof num; </script> </body> </html>
Conversion to Integer Using Math.floor Method
In this approach to convert string to integer in JavaScript, we have used Math.floor method. It converts string to integer through type coercion and then rounds the integer to nearest integer value.
Example
Here is an example implementing Math.floor method to convert string to integer.
<html> <body> <div id="str"></div> <div id="num"></div> <script> let x=document.getElementById("str"); let str = "123.2"; x.innerHTML = "Input String: " + str + ", It is of type: " +typeof str; let res=document.getElementById("num"); let num = Math.floor(str); res.innerHTML = "Result: " + num + ", It is of type: " +typeof num; </script> </body> </html>
Conversion to Integer Using Math.round Method
In this approach to convert string to integer in JavaScript, we have used Math.round method. It converts string to integer through type coercion and then rounds the integer to nearest integer value considering decimal 0.5 as critical point.
Example
Here is an example implementing Math.round method to convert string to integer.
<html> <body> <div id="str"></div> <div id="num"></div> <script> let x=document.getElementById("str"); let str = "123.6"; x.innerHTML = "Input String: " + str + ", It is of type: " +typeof str; let res=document.getElementById("num"); let num = Math.round(str); res.innerHTML = "Result: " + num + ", It is of type: " +typeof num; </script> </body> </html>
Conversion to Integer Using Math.ceil Method
In this approach to convert string to integer in JavaScript, we have used Math.ceil method. It converts string to integer through type coercion and then rounds the integer to nearest upward integer value even if decimal value is too small.
Example
Here is an example implementing Math.ceil method to convert string to integer.
<html> <body> <div id="str"></div> <div id="num"></div> <script> let x=document.getElementById("str"); let str = "123.01"; x.innerHTML = "Input String: " + str + ", It is of type: " +typeof str; let res=document.getElementById("num"); let num = Math.ceil(str); res.innerHTML = "Result: " + num + ", It is of type: " +typeof num; </script> </body> </html>
Conversion to Integer Using Math.trunc Method
In this approach to convert string to integer in JavaScript, we have used Math.trunc method. It returns the integer part of the number by truncating the decimal digits.
Example
Here is an example implementing Math.trunc method to convert string to integer.
<html> <body> <div id="str"></div> <div id="num"></div> <script> let x=document.getElementById("str"); let str = "123.123"; x.innerHTML = "Input String: " + str + ", It is of type: " +typeof str; let res=document.getElementById("num"); let num = Math.trunc(str); res.innerHTML = "Result: " + num + ", It is of type: " +typeof num; </script> </body> </html>
Conversion to Integer Using BigInt Constructor
In this approach to convert string to integer in JavaScript, we have used BigInt constructor. It converts string to BigInt and throws error if input string is not a valid whole number i.e if it contains decimal or non-numeric character.
Example
Here is an example implementing BigInt constructor to convert string to integer.
<html> <body> <div id="str"></div> <div id="num"></div> <script> let x=document.getElementById("str"); let str = "123444444444"; x.innerHTML = "Input String: " + str + ", It is of type: " +typeof str; let res=document.getElementById("num"); let num = BigInt(str); res.innerHTML = "Result: " + num + ", It is of type: " +typeof num; </script> </body> </html>
Conclusion
To convert string to integer in JavaScript is a very simple and easy task. In this article, we have discussed eight approaches which are: by using parseInt() method, bitwise operator, double tilde operator, Math.floor() method, Math.round() method, Math.ceil() method, Math.trunc method and by using BigInt constructor.