Number toString() Method with Example in JavaScript

JavaScript Number toString() Method: Here, we are going to learn about the toString() Method of Number class with Example.
Submitted by IncludeHelp, on February 20, 2019

Number toString() Method

toString() method is a method of Number class, it is used to convert a number to the string. It can also be used to convert a given number to the string with specified number systems like binary string, octal string, and hexadecimal string.

Syntax:

    Number.toString([radix]);

Here, radix is an optional parameter, it can be used to define the base of the number system in which we have to convert the number as a string.

Examples:

    Input: 10
    num = 75

    Output:
    num.toString() - "75"
    num.toString(2) - "1001011"
    num.toString(8) - "113"
    num..toString(16) - "4B"

Code:

<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
	var num = 75;
	
	var str1 = num.toString();
	var str2 = num.toString(2);
	var str3 = num.toString(8);
	var str4 = num.toString(16);
	
	document.write("str1 = " + str1 + "<br>");
	document.write("str2 = " + str2 + "<br>");
	document.write("str3 = " + str3 + "<br>");
	document.write("str4 = " + str4 + "<br>");
	
</script>
</body>
</html>

Output

str1 = 75
str2 = 1001011
str3 = 113
str4 = 4b

JavaScript Number Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.