Special Use of parentheses () and square brackets [] in JavaScript _ basic knowledge
Source: Internet
Author: User
You may have some questions about the special usage of parentheses () and square brackets [] in JavaScript. This article will introduce these questions in detail, do not miss out on friends who have doubts (1, 2, 3 );
// Return 3; curious, why does it return 3? What mechanism is this? What is the role of parentheses?
(1, 2, 3, alert) ("Amazing! ");
// This is even more fun. You can call alert directly. After testing, global functions can be called in this way.
The following questions about square brackets:
In JS, [] is almost equal to the array, but there is also a magic time.
[1, 2, 3] [1];
// Return 2; of course, this method is also frequently used. It is often used when the index of an array and the response attribute name of the called object are stored in a variable.
[1, 2, 3] [1, 2];
// Return 3; this situation is much more interesting. It is similar to the situation in the parentheses above and returns the last value in the brackets.
In (, 3), brackets and commas are all operators. The comma operator returns the result of the evaluation of the last expression. You can check the specific rules by yourself.
("Amazing") brackets indicate function calls.
Alert is a function object. When it is returned as an expression evaluate result, it can certainly be called.
[, 3] square brackets are used to declare an array constant, followed by [1] to retrieve array elements. [, 3] [1] indicates the first element in the array [, 3], that is, 2 (subscript starts from 0)
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