What is the role of parentheses () in JS?

I know that there are many functions of parentheses () in JS, but currently only discusses the examples given below

10.toString()    // Invalid or unexpected token
(10).toString()  // "10"

{}+""           // 0
({})+""         // "[object Object]"
Apr.06,2022

10.toString()   // 
(10).toString() // 5 toString

{}+''           // {} +''
({})+''         // {}   +''

in your example, simply using () is equivalent to defining a immediate execution function , and the callback object is the content wrapped by ()

.
(10) //10
({}) //{}

with this feature, you can also add parameters to this function

(v => v + 10)(10); //20

We also often use this feature to simplify our code

[1, 2, 3].map(v => {
  return {value: v};
});

can be simplified to the following

[1, 2, 3].map(v => ({value: v}))

. : is the decimal point and is also the attribute accessor .
{} : is code block and is also object .

parentheses, like parentheses in mathematical operations, obediently define the priority (highest) of the operator. Avoid ambiguity like the one above.


operators like this. The most direct way to find out all its features is to consult the specification.

http://www.ecma-international...


brackets can enhance the expression of meaning and reduce ambiguity.


assigns the literal quantity to the anonymous variable


{} +''and ({}) +' 'return the same, both [object Object]

Menu