3 Example(s) of Javascript Number


Description :

Number function convert value to number. In this example, we declare multiple variable like: var a = false; var b = true; var c = new Date(); var d = "888"; var e= "666 555"; and will get result into 0 1 1450198829985 888 NaN


Javascript Number Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="convertToNumber()">Convert variables to numbers</button>
<p id="sample"></p>
<script>
function convertToNumber() {
    var a = false;
    var b = true;
    var c = new Date();
    var d = "888";
    var e= "666 555";
    var result = 
    Number(a) + "<br>" + 
    Number(b) + "<br>" + 
    Number(c) + "<br>" + 
    Number(d) + "<br>" + 
    Number(e);
    document.getElementById("sample").innerHTML = result;
}
</script>
</body>
</html>

Output

Description :

In this example, Number.MAX_VALUE will print the maximum value of number. See the code snippet:


Javascript Number Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="largestNumber()">Show the largest number</button>
<p id="sample"></p>
<script>
function largestNumber() {
    document.getElementById("sample").innerHTML = Number.MAX_VALUE;
}
</script>
</body>
</html>

Output

Description :

In this example, Number.MIN_VALUE will print the maximum value of number. See the code snippet:


Javascript Number Example - 3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="smallestNumber()">Show the smallest number</button>
<p id="sample"></p>
<script>
function smallestNumber() {
    document.getElementById("sample").innerHTML = Number.MIN_VALUE;
}
</script>
</body>
</html>

Output