toString() returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or expressions).
To convert into binary, toString() function has 2 as parameter.
To convert into octal, toString() function has 8 as parameter.
To convert into hexadecimal, toString() function has 16 as parameter.
JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.
Code:-
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>tostring() Hex,Octal and Binary</h1>
<input type="button" onclick="hob();" value="Get Answer">
<script>
function hob()
{
var n1 = 998;
alert("Number : " + n1.toString());
alert("Binary : " + n1.toString(2));
alert("Octal : " + n1.toString(8));
alert("Hex : " + n1.toString(16));
}
</script>
</body>
</html>
No comments: