JS Set function parameter default value (suitable for no reference) _javascript tips

Source: Internet
Author: User
Today to write a div+css analog JS information box class Library encountered a problem, I hope that when I did not pass the parameters of the past, automatically use the "prompt information" as the window title. I tried to write this.
Copy Code code as follows:

function MessageBox (title= "") {
}

There is no doubt that I failed to run (otherwise I would not send this blog)

Finally after a Baidu, found such a good thing
Copy Code code as follows:

function Test (a) {
var b=arguments[0]?arguments[0]:50;
Return a + ': ' +b;
}

According to my humble understanding, arguments is probably something like an array, with the subscript starting from 0, which represents the parameter of the function.

For example, the arguments[0 in the example above represents the parameter a

In fact, arguments[0]?arguments[0]:50 can also be written: Arguments[0] | | 50; The wording is quite concise. Above, is the method of setting the function parameter default value in JS

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.