-1

I'm taking a course on JavaScript but have no guide on toString method, what's the purpose of these two outputs in JavaScript:

  1. (35).toString(36) >>> "z"!!!

  2. (35).toString(37) >>> throws a RangeError!!!

I am utterly confused as to why I am getting these results, I would really appreciate it if someone could shed some light on this.

doubleOrt
  • 2,407
  • 1
  • 14
  • 34
  • Please read the [documentation on `Number.prototype.toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString) and then [edit] your question so that you've asked a well-researched and specific question. – 4castle Oct 12 '17 at 04:29
  • @4castle If it isn't a duplicate, and it isn't too beginnerish, then subjectively, I consider it a legitimate question for StackOverflow. – doubleOrt Nov 24 '17 at 21:42
  • @Taurus It's a legitimate question. Being low-quality isn't a close reason. It's a downvote reason. – 4castle Nov 24 '17 at 23:09
  • @4castle Why do you consider it low-quality ? – doubleOrt Nov 25 '17 at 11:08

2 Answers2

2

tldr: Number.prototype.toString can take an argument called radix that specifies the numeric base to use for the string representation of the number in question.


Object.prototype.toString()

An object's toString method is supposed to return a string representation of that object (I say "supposed" because you can modify it so it doesn't return a string). 35 is clearly not an object, it is a primitive, but you are using it like an object, which causes JavaScript to create a temporary Number object for that toString call (see this StackOverflow answer on autoboxing).


Number.prototype.toString()

About the confusing behavior you are getting by passing 36 to (35).toString: it is because Number.prototype.toString can take an argument that you can use to specify the numeric base to use for the string representation of that number, the argument must be an integer (or any other value that can be coerced to an integer, e.g 35..toString([20])) between 2 and 36, so 2 <= [radix] <= 36 (this means your second example will throw a RangeError).

So, when you execute (35).toString(36), 2 things happen (not necessarily in my order, and most likely it is done in a single step, 35 ====> [string representation of 35 in numeric format specified by "radix"]):

  1. Generate a string representation of the number 35.
  2. Convert the string generated in step #1 to the number base specified by radix.

For example, if you wanted a string representation of 35, in binary form:

console.log(35..toString(2)); // "100011"

Fun fact: the syntax [integer]..[method] is totally valid in JavaScript, the first . is interpreted as a decimal point, the latter as the . that precedes the name of an object's method/property.


Radix

If you don't know what a "radix" is (because I didn't prior to this question, and no, I am no caveman, English is not my native language), here is the definition I got by a Google search for "radix meaning":

the base of a system of numeration.

Sébastien
  • 11,860
  • 11
  • 58
  • 78
doubleOrt
  • 2,407
  • 1
  • 14
  • 34
-1

toString converts an Int to a string. An int is used for math and a string is used as text and math should not be done on it.

Maybe a little more of the code you are looking at would shed more light on what is going on in the script

Brént Russęll
  • 756
  • 6
  • 17
  • these are questions my instructor gave me but I don't know what either of these two questions are asking. thanks for clarifying on what strings do. – 1vrxtc9 Oct 12 '17 at 04:28
  • Please try out the examples OP provided (namely, the first one). – doubleOrt Nov 24 '17 at 21:43