Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean when there is a number parameter passed to toString?

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.

like image 374
Dennis D Avatar asked Apr 13 '10 16:04

Dennis D


People also ask

How many parameters are required for toString?

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.

How are numbers converted to 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.

What does toString () do in JavaScript?

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.

How many parameters are required in the toString method in JavaScript?

Parameters. An integer in the range 2 through 36 specifying the base to use for representing numeric values.


2 Answers

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" 
like image 199
Gumbo Avatar answered Sep 28 '22 08:09

Gumbo


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.

like image 25
Tomalak Avatar answered Sep 28 '22 07:09

Tomalak