I assume you are asking a question because you saw this effect in JavaScript REPL (shell). You are using a JavaScript shell that assumes that the leading "{" starts the block statement instead of the object literal.
For example, if you use the JavaScript interpreter that comes with the Chrome browser, you will see the following:
> {key:"value"}["key"] ["key"]
Here, Chrome saw what you entered as a block, and then an expression that was an array of one element, the string "key" . Therefore, he responded with the result of this expression, namely: array ["key"]
But not all sinks work this way. If you use the interpreter with node.js, then # 1 will work for you!
$ node > {key:"value"}["key"] 'value' >
In interpreters like Chrome, you should use parentheses to say that you want the first part to be an object literal. (This method, by the way, is guaranteed to work in all shells, including node).
EDIT
As stated in one comment, if you use this construct in the context of an expression somewhere in the actual script, it will produce "value" . This is a shell usage that looks confusing.
This fact was actually used in the famous WAT video by Gary Bernhardt.
source share