> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/func/arrow/return-obj-literal.md).

# arrow function returning object literal needs (...)❗️

🚧 under construction

[JS](/web/js.md) ⟩ [objects](/web/js/val/obj.md) ⟩ [arrow function](/web/js/val/func/arrow.md) ⟩ returning object literal needs (...)

{% hint style="danger" %}

```javascript
const user = () => { age: 2019 };    // ❌ 
const user = () => ({ age: 2019 });  // ✅ 
```

{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}

* the following code：

```javascript
const user = () => { name: 'Gandalf', age: 2019 };
```

* is interpreted as：

```javascript
const user = () => { 
    name: 'Gandalf', age: 2019 
    //  ↳ ⛔️ SyntaxError: Unexpected token ':'
};
```

* which <mark style="color:yellow;">**causes an error**</mark>：⛔️ <mark style="color:red;">SyntaxError: Unexpected token ':'</mark>, have to <mark style="color:yellow;">**wrap**</mark> the [object literal](/web/js/val/obj/create/obj.md) <mark style="color:yellow;">**in parentheses**</mark> to make it work.

```javascript
// ⭐️ wrap object literal in parentheses
const user = () => ({ 
    name: 'Gandalf', 
    age: 2019 
});

console.log(user());         // { name: 'Gandalf', age: 2019 }
```

:floppy\_disk: replit：[arrow function returning object literal](https://replit.com/@pegasusroe/return-object-literal-from-arrow-function#index.js)
{% endtab %}

{% tab title="👥 相關" %}

* [⛔️ SyntaxError](/web/js/err/syntax.md) ⟩ [unexpected token "xxx"❗️](/web/js/err/syntax/unexpected-token.md)
  {% endtab %}
  {% endtabs %}
