skip to Main Content
console.log(123['toString'].length);

why this code returns value: 1 ?

Any good explanation?

2

Answers


  1. 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:

    123  => number
    ['toString'] => function toString of number
    .length => length property of the function
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search