# A JavaScript Interview Question That 90% of People Get Wrong

Let’s take a look at the question first:

123['toString'].length + '123' = ?

Did you think of an answer immediately? Hold on before you respond. Perhaps you’re already familiar with type conversions, but how do we calculate the length of the `toString`

function? And how is the length of a normal function calculated?

In JavaScript, the `length`

property of a function represents the number of arguments expected by the function when it was declared. It counts the number of expected arguments, not including the rest parameters operator (`...`

) after it, nor considering default parameters. This property is often used in reflection and some functional programming scenarios to understand how many arguments a function expects to receive.

## Example Explanations

1.**Function with No Parameters**

```
function noParams() {}
console.log(noParams.length); // Outputs: 0
```

This function defines no parameters, so its `length`

property is 0.

2. **Function with One Parameter**

```
function oneParam(a) {}
console.log(oneParam.length); // Outputs: 1
```

This function defines one parameter, so its `length`

property is 1.

3. **Function** **with Multiple Parameters**

```
function multipleParams(a, b, c) {}
console.log(multipleParams.length); // Outputs: 3
```

This function defines three parameters, so its `length`

property is 3.

4. **Function with Default Parameters**

```
function defaultParams(a, b = 2, c) {}
console.log(defaultParams.length); // Outputs: 1
```

When a function has default parameters, the `length`

property counts up to the first default parameter. Although three parameters are defined in this example, because the second parameter is a default parameter, the `length`

value is 1.

5.** Function with Rest Parameters**

```
function restParams(a, b, ...rest) {}
console.log(restParams.length); // Outputs: 2
```

For functions using the rest parameters operator, the `length`

property counts only the parameters before the rest parameter. In this example, although the function can accept an infinite number of arguments, the `length`

value is 2, counting only the parameters before `...rest`

.

These examples show that a function’s `length`

property provides a simple way to see how many arguments a function expects to receive, which can be useful in some programming operations.

In JavaScript, every number inherits methods from the `Number`

prototype, including `toString`

. When you access `123['toString']`

, you're actually accessing the `toString`

method of the number `123`

. The `.length`

property of a function in JavaScript indicates the number of expected arguments, and since `Number.prototype.toString`

can take one argument (the radix), its `.length`

is `1`

.

Therefore, in JavaScript:

`123['toString'].length`

would evaluate to`1`

.- Adding
`123`

to this gives`1 + '123'`

, which equals '`1123'`

.

So, `123['toString'].length + '123'`

would result in '`1123'`

.

# In Plain English 🚀

*Thank you for being a part of the In Plain English community! Before you go:*

- Be sure to
**clap**and**follow**the writer ️👏**️️** - Follow us:
**X****| LinkedIn | YouTube | Discord | Newsletter** - Visit our other platforms:
**Stackademic****| CoFeed | Venture | Cubed** - More content at
**PlainEnglish.io**