Javascript: optional parameter of the Function

Source: Internet
Author: User

Javascript functions can be called with any number of parameters, regardless of the number of parameter names in the function definition. Because a function is of a loose type, it cannot declare the type of the expected parameter, and it is legal to pass any type of parameter to any function.

When the number of parameters that call a function is less than the number of declared parameters, other parameters have an undefined value. Writing such a function is often useful: some parameters are optional and can be ignored when calling the function. To do this, you must be able to assign a reasonable default value for ignored parameters. For example:

<HTML>  

In this example, if a has been defined and is not null, it returns a, even if a is null. Otherwise, it returns a new empty array.

When using optional parameters to design functions, make sure that the optional parameters are placed at the end of the parameter list so that they can be ignored.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.