parseInt function will convert string to Integer value with specified radix. See code snippet below:
Javascript parseInt functions Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="convertToInt()">Parse string to integer</button>
<p id="sample"></p>
<script>
function convertToInt() {
var a = parseInt("10") + "<br>";
var b = parseInt("10.00") + "<br>";
var c = parseInt("10.33") + "<br>";
var d = parseInt("34 45 66") + "<br>";
var e = parseInt(" 60 ") + "<br>";
var f = parseInt("40 years") + "<br>";
var g = parseInt("He was 40") + "<br>";
var h = parseInt("10", 10)+ "<br>";
var i = parseInt("010")+ "<br>";
var j = parseInt("10", 8)+ "<br>";
var k = parseInt("0x10")+ "<br>";
var l = parseInt("10", 16)+ "<br>";
var result = a + b + c + d + e + f + g + "<br>" + h + i + j + k +l;
document.getElementById("sample").innerHTML = result;
}
</script>
</body>
</html>
In this example, We will sum the value of two variable a and b and show in paragraph inner html, See code snippet:
Javascript parseInt functions Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="convertToInt()">Parse the string to integer</button>
<p id="sample"></p>
<script>
function convertToInt() {
var a = parseInt("10");
var b = parseInt("10.00");
var result = a + b;
document.getElementById("sample").innerHTML = result;
}
</script>
</body>
</html>