I'm just wondering what it means to attach a number as a parameter to the toString()
method
E.g. obj.toString(10);
I googled and i have never seen a parameter before.
Parameter: The method accepts two parameters: a: This is of integer type and refers to the integer value that is to be converted. base: This is also of integer type and refers to the base that is to be used while representing the strings.
The toString() method is a built-in method of the JavaScript Number object that allows you to convert any number type value into its string type representation.
toString . For user-defined Function objects, the toString method returns a string containing the source text segment which was used to define the function. JavaScript calls the toString method automatically when a Function is to be represented as a text value, e.g. when a function is concatenated with a string.
Parameters. An integer in the range 2 through 36 specifying the base to use for representing numeric values.
The additional parameter works only for Number.prototype.toString
to specify the radix (integer between 2 and 36 specifying the base to use for representing numeric values):
var number = 12345; number.toString(2) === "11000000111001" number.toString(3) === "121221020" // … number.toString(36) === "9ix"
This only works on Number objects and is intended to give you a way of displaying a number with a certain radix:
var n = 256; var d = n.toString(10); // decimal: "256" var o = n.toString(8); // octal: "400" var h = n.toString(16); // hex: "100" var b = n.toString(2); // binary: "100000000" var w = n.toString(20); // base 20: "cg"
Note that the radix must be an integer between 2 and 36 or toString()
will raise an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With