avatarfatfish

Summary

The web content discusses a JavaScript interview question that many front-end developers find challenging: determining the output of (123['toString'].length + 123) and explaining the function.length property in various scenarios, including default parameters and rest parameters.

Abstract

The article titled "What does (123['toString'].length + 123) Print Out in JavaScript?" addresses a seemingly simple JavaScript question that stumps a majority of front-end developers. Initially, the author admits to incorrectly guessing the output, highlighting the common misconception. The article delves into the intricacies of JavaScript functions, particularly focusing on the function.length property. It clarifies how function.length reflects the number of formal parameters, excluding those with default values and rest parameters. Through a series of examples and explanations, the author unravels the mystery behind the property, providing insights into the correct interpretation of function parameters in JavaScript. The article concludes by summarizing the key points and directing readers to the Mozilla Developer Network (MDN) for further reading. It also encourages readers to engage with additional content on the site, subscribe to newsletters, and follow social media channels for more insights and discussions in the web development community.

Opinions

  • The author suggests that the initial intuition about the JavaScript code output is often incorrect, emphasizing the question's difficulty.
  • There is an underlying assumption that understanding the function.length property is crucial for front-end developers.
  • The article implies that developers should be aware of JavaScript's nuances, such as the behavior of default and rest parameters in relation to the function.length property.
  • By presenting the topic as an interview question, the author insinuates that this knowledge could be a differentiator in job interviews.
  • The author seems to value the MDN as a credible source for developers to deepen their understanding of JavaScript's function properties.
  • The article promotes the idea that continuous learning and engagement with the developer community are important for professional growth.

What does (123[‘toString’].length + 123) Print Out in JavaScript?

A question that 95% of front-end developers answer incorrectly.

Preface

Some time ago, I was asked an easy interview question but I didn’t give the right answer, in the beginning, even 95% of front-end developers couldn’t answer it accurately. What is the interview question? Let’s have a look…

At first, I thought the answer was 126(3 + 123), but I was wrong. What is the real answer? Let's unravel the mystery step by step.

The number of formal parameters of the function

This is very easy. All front-end developers know the answer.

function has as many parameters as its length. But is this really the case? Follow me and keep looking down...

Default parameters

It shocked me and it even looks like there are some differences from the answers above. Yes, we can draw a conclusion from here:

function.length is the number of parameters before the first one with a default value.

The remaining parameters of the function

If a function has remaining parameters, how will “function.length” be calculated?

Yes, I’m sure you’re right. The answer is 1.So we can draw another conclusion:

The remaining parameters are not included in the calculation of “function.length”.

Summary

Finally, do you know the answer to this question?

Take a look at the summary on MDN

Length is a property of a function object and indicates how many arguments the function expects, i.e. the number of formal parameters.

This number excludes the rest parameter and only includes parameters before the first one with a default value.

By contrast, arguments.length is local to a function and provides the number of arguments actually passed to the function.

Finally

Thanks for reading. Looking forward to your following and reading more high-quality articles.

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

Front End Development
JavaScript
Programming
React
Vue
Recommended from ReadMedium