The two first examples you show are semantically identical. In both cases, foo
is assigned an object literal with a Method definition. The braces don't make a difference. As comments suggest, var foo = 1;
is identical to var foo = (1);
. In the third example, they are also not necessary. Note, that they also are not a mistake.
There are examples, where such braces make a difference, but they are not the ones you show. Some that come to mind:
Returning an object from an arrow function, shorthand version: let foo = () => ({});
, differentiates from an empty code block.
Differentiating a code block from a stand-alone object, e.g. in the console: {} + [];
vs ({}) + [];
IIFE, turning a function declaration statement into an expression: (function f(){})();
, whereas function f(){}();
would result in an error.