C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Number parseInt() methodThe JavaScript number parseInt() method parses a string argument and converts it into an integer value. With string argument, we can also provide radix argument to specify the type of numeral system to be used. SyntaxThe parseInt() method is represented by the following syntax: Number.parseInt(string, radix) Parameterstring - It represents the string to be parsed. radix - It is optional. An integer between 2 and 36 that represents the numeral system to be used. ReturnAn integer number. It returns NaN, if the first character cannot be converted to a number. JavaScript Number parseInt() method exampleHere, we will understand parseInt() method through various examples. Example 1Let's see a simple example of parseInt() method. <script> var a="50"; var b="50.25" var c="String"; var d="50String"; var e="50.25String" document.writeln(Number.parseInt(a)+"<br>"); document.writeln(Number.parseInt(b)+"<br>"); document.writeln(Number.parseInt(c)+"<br>"); document.writeln(Number.parseInt(d)+"<br>"); document.writeln(Number.parseInt(e)); </script> Output: 50 50 NaN 50 50 Example 2Let's see an example to add two strings with and without using parseInt() method. <script> var a="10"; var b="20"; var c=a+b; document.writeln("Before invoking parseInt(): "+c+"<br>"); var c=Number.parseInt(a)+Number.parseInt(b); document.writeln("After invoking parseInt(): "+c); </script> Output: Before invoking parseInt(): 1020 After invoking parseInt(): 30 Example 3In this example, we will pass radix argument within parseInt() method. <script> var a="50"; document.writeln(Number.parseInt(a,10)+"<br>"); document.writeln(Number.parseInt(a,8)+"<br>"); document.writeln(Number.parseInt(a,16)); </script> Output: 50 40 80
Next TopicJavaScript Math
|