What is the length of a function in js?

What is the length of a function in js?

Preface

Today I will tell you how to calculate the length of a function. I hope everyone can learn something from it and consolidate their basics.

Why

Why did I think of this knowledge point? Because last night, in a group, a classmate was discussing an interview question from ByteDance.

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

To be honest, I couldn't answer this question at first. Actually, I knew that the interviewer wanted to test the toString method on the Number prototype, but I was stuck on the difficult question of what is the length of the toString function. That’s why I have this article today.

How much is it?

Number of parameters

Let's look at the following example

function fn1 () {}

function fn2 (name) {}

function fn3 (name, age) {}

console.log(fn1.length) // 0
console.log(fn2.length) // 1
console.log(fn3.length) // 2

It can be seen that the length is the same as the number of parameters of the function. But is this really the case? Continue reading

Default Parameters

What would be the length of the function if there were default parameters?

function fn1 (name) {}

function fn2 (name = '林三心') {}

function fn3 (name, age = 22) {}

function fn4 (name, age = 22, gender) {}

function fn5(name = '林三心', age, gender) { }

console.log(fn1.length) // 1
console.log(fn2.length) // 0
console.log(fn3.length) // 1
console.log(fn4.length) // 1
console.log(fn5.length) // 0

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

Rest parameters

There are also rest parameters in the function's formal parameters. How are they calculated if there are rest parameters?

function fn1(name, ...args) {}

console.log(fn1.length) // 1

It can be seen that the remaining parameters are not included in the calculation of length

Summarize

Before we conclude, let me first announce that the answer to 123['toString'].length + 123 = ? is 124.

To summarize, length is a property value of a function object, which refers to how many parameters must be passed into the function, that is, the number of formal parameters. The number of formal parameters does not include the remaining parameters, only the number of parameters before the first one with a default value.

This is the end of this article about the length of functions in js. For more information about the length of js functions, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • An article teaches you JS function inheritance
  • JavaScript Basics Series: Functions and Methods
  • Use of JavaScript sleep function
  • Exception handling of built-in functions json_encode and json_decode in php
  • How to make your JavaScript functions more elegant
  • Summary of 50+ Utility Functions in JavaScript
  • In-depth understanding of JavaScript callback functions
  • Three solutions for sub-functions accessing external variables in JavaScript
  • JavaScript functional programming basics

<<:  Detailed explanation of the solution to the problem of automatic disconnection of xshell remote connection

>>:  Detailed explanation of various practical uses of virtual device files in Linux system

Recommend

3 different ways to clear the option options in the select tag

Method 1 Copy code The code is as follows: documen...

JavaScript to achieve progress bar effect

This article example shares the specific code of ...

How to modify mysql permissions to allow hosts to access

Enable remote access rights for mysql By default,...

A brief talk about React Router's history

If you want to understand React Router, you shoul...

Explanation of the precautions for Mysql master-slave replication

1. Error error connecting to master 'x@xxxx:x...

Vue implements the frame rate playback of the carousel

This article example shares the specific code of ...

CSS to implement sprites and font icons

Sprites: In the past, each image resource was an ...

js implements a simple countdown

This article example shares the specific code of ...

Building an image server with FastDFS under Linux

Table of contents Server Planning 1. Install syst...

HTML+CSS project development experience summary (recommended)

I haven’t updated my blog for several days. I jus...

Use jQuery to fix the invalid page anchor point problem under iframe

The application scenario is: the iframe page has n...