console.log(123['toString'].length);
why this code returns value: 1 ?
Any good explanation?
2
Its the expected number of parameters of the toString function.
toString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length
The statement is interpreted like this:
123 => number ['toString'] => function toString of number .length => length property of the function
Because in javascript the length of a function is the number of named arguments it accepts.
Your 123['toString'] returns the toString method of numbers which accepts a radix parameter (see Number.prototype.toString), so that is what 1 is for.
123['toString']
radix
Number.prototype.toString
Click here to cancel reply.
2
Answers
Its the expected number of parameters of the
toString
function.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length
The statement is interpreted like this:
Because in javascript the length of a function is the number of named arguments it accepts.
Your
123['toString']
returns thetoString
method of numbers which accepts aradix
parameter (seeNumber.prototype.toString
), so that is what 1 is for.