DEV Community

Discussion on: Key JavaScript Concepts I Mastered During LeetCode’s 30-Day Challenge

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

You need be careful with the length property of a function. It actually returns the number of parameters (not arguments - those are what we pass in) a function accepts, but only the number up to the first parameter that doesn't have a default value assigned:

console.log( (a => a).length )  // 1
console.log( ((a, b) => a).length )  // 2
console.log( ((a, b, c=0) => a).length )  // 2
console.log( ((a, b, c=0, d) => a).length ) // 2
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ilatif profile image
Imran Latif

Yes, you are correct. In my case, all I needed was a way to get number of parameters a function is accepting to solve a specific LeetCode problem. But yes, one should be careful while using length property on functions due to the reasons you've explained with examples.

Thanks!