All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Convert String To Int In JavaScript

Last Updated : Mar 11, 2024

How To Convert String To Int In JavaScript

In this article we will show you the solution of how to convert string to int in JavaScript, in JavaScript, we can convert string to integer by using different functions.

Let us see all the methods we are going to use in this tutorial.

parseInt() : this function in JavaScript, parses a string and returns an integer.

Syntax: parseInt (value, radix)

Number() : the number() function in JavaScript used to convert values to an integer

Syntax: Number (value)

‘+’ operator : using ‘+’ operator we can also convert any string to an integer.

‘ ~~ ‘ operator : using the double tilde(~~) operator before the string can also be used

Math.round() : the math.round() function is used to convert a number to the nearest integer.

Syntax: Math.round (value)

Math.floor() : this function is used to convert a floating point number to its nearest integer

Syntax: Math.floor (value)

Step By Step Guide On How To Convert String To Int In JavaScript :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> how to convert string to int in javascript </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
        <h1> WELCOME TO TALKERSCODE </h1>
        <h3> how to convert string to int in javascript </h3>
        </center>
    <script>
        let number1 = parseInt("1000") ;
        console.log(typeof number1) ;
        let number2 = Number("2000") ;
        console.log(number2) ;
        let number3 = +"3000" ;
        console.log(number3) ;
        let number4 = ~~"4000" ;
        console.log(number4) ;
        let number5 = Math.round("5000") ;
        console.log(number5) ;
        let number6 = Math.floor("6000.01") ;
        console.log(number6) ;
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> to mention the version of the HTML file
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Attach an external CSS file using <link> tag
  5. <h1> tag used to add heading here and also adding the inline CSS here.
  6. Let a variable number1 with parseInt().
  7. Using a console.log() with typeof number1 to display the number type.
  8. Let a variable number2 with the Number() function.
  9. Using console.log() to display the integer function
  10. Let a variable number3 with ‘+’ operator.
  11. Using console.log() to display the integer function
  12. Let a variable number4 with ‘~~’ operator.
  13. Again using console.log() to display the string as integer
  14. Let a variable number5 with math.round() function.
  15. Again using console.log() to display the string as integer
  16. Let a variable number6 with math.floor() function.
  17. Using console.log() to display the integer function

Conclusion :-

At last, here in conclusion, we can say that with this article’s help, we know how to convert string to int using JavaScript.

I hope this article on how to convert string to int in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪