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]

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea693c-4686b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea693c-4686b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?