JavaScript in parentheses () and square brackets [] special usage questions and Answers _ basics

Source: Internet
Author: User
(1, 2, 3);
return 3; Very curious, why return 3? What kind of mechanism is this? What is the effect of parentheses?

(1, 2, 3, alert) ("amazing!");
It's even more fun to call alert directly, which can be invoked by a global function.

The following are questions about square brackets []:
In JS, [] is almost equal to several paintings, but there are also magical times.

[1,2,3] [1];
return 2; Of course, this method is also used frequently, the index of the array and the response property name of the invoked object are stored in a variable.
[1,2,3] [1,2];

return 3; This is a lot more interesting, like the parentheses above, which returns the last value in parentheses.

In (1,2,3), parentheses and commas are operators, and the comma operator returns the result of the last expression, and the specific rules can be found by themselves.
("amazing") parentheses represent a function call.
Alert is a function object that, when returned as a result of an expression, can of course be invoked.

[1,2,3] Here the square brackets are declared array constants, immediately following [1] to represent the array element. [1,2,3] [1] means to take the first element in the array [1,2,3], that is, 2 (subscript starting from 0)

[1,2,3] [1,2]=[1,2,3][(1,2)]=[1,2,3][2]=3
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.