Home > Web Front-end > JS Tutorial > body text

Questions and Answers on the Special Usage of Parentheses () and Square Brackets [] in JavaScript_Basic Knowledge

WBOY
Release: 2016-05-16 17:26:38
Original
1572 people have browsed it

(1, 2, 3);
// return 3; I’m curious, why does it return 3? What is the mechanism? What role do parentheses play?

(1, 2, 3, alert )("Amazing!");
// What’s even more fun here is that alert can be called directly; after testing, global functions can be called like this.

The following are questions about square brackets []:
In JS, [] is almost equal to the array, but there are also magic moments.

[1,2,3][1];
// return 2; Of course, this method is also often used. It is often used for the index of the array and the response attribute name of the calling object to be stored in a certain variable.
[1,2,3][1,2];

//return 3 ; This case is much more interesting. It is very similar to the case of parentheses above, and will return the last character in the parentheses. A value

in (1,2,3), brackets and commas are both operators. The comma operator returns the evaluation result of the last expression. You can check the specific rules yourself. The brackets in
("Amazing") represent function calls.
alert is a function object that can of course be called when it is returned as the evaluation result of an expression.

[1,2,3] The square brackets here are to declare array constants, followed by [1] to take array elements. [1,2,3][1] means taking the first element in the array [1,2,3], that is, 2 (the subscript starts from 0)

[1,2,3 ][1,2]=[1,2,3][(1,2)]=[1,2,3][2]=3

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template