JavaScript parseInt | Convert a String to Integer Number

While working with numbers in javascript, it is necessary to know what you can do with them, but the most important thing is how to do it.

This Javascript tutorial will demonstrate parseInt() function, you will learn what is parseInt in javascript and what it does. You will not only see many examples, but you will learn from them. After finishing this example, you will be familiar with JavaScript Parsing () syntax and where you should use it.

Convert a String to Integer Number in javaScript

  • About JavaScript parseInt() Function
  • JavaScript parseInt() Function Syntax
  • Examples of Convert a String to Integer Number in javascript

About JavaScript parseInt() Function

The javascript parseInt () function returns an integer number by parse the string. If the first character can not be changed in number, the function returns NaN. JavaScript parseInt simple convert a string to an integer number

JavaScript parseInt() Function Syntax

The syntax of javascript parseInt () is very simple. The JavaScript parseInt () should look like this :

parseInt(string, radix)
  • string Needed. The string you want to be parsed
  • radix Not required. The numeral system representing a number.

Examples of Convert a String to Integer Number in javascript

To understand what parseInt is in JavaScript, it is necessary to see an example. Take a look at this parseInt function example – where we parse different numbers and strings :

<script>
    // returns a Integer no
    a = parseInt("20");
    document.write('parseInt("20") = ' + a + "<br>");
    //  Not a Number character
    b = parseInt("30.00");
    document.write('parseInt("30.00") = ' + b + "<br>");

    // returns NaN on Non numeral character
    c = parseInt("2.366");
    document.write('parseInt("2.366") = ' + c + "<br>");

    // returns Integer value of a Floating point Number
    d = parseInt("3.14");
    document.write('parseInt("3.14") = ' + d + "<br>");

    // only first Number it encounters
    e = parseInt("22 3 2019");
    document.write('parseInt("22 3 2019") = ' + e + "<br>");

</script>

Output

 
parseInt("20") = 20
parseInt("30.00") = 30
parseInt("2.366") = 2
parseInt("3.14") = 3
parseInt("22 3 2019") = 22

Recommended JavaScript Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *