---
description: 🚧 under construction
---

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

[JS](../../..) ⟩ [objects](../../obj) ⟩ [arrow function]() ⟩ 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 [obj](../../obj/create/obj "mention") <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="👥 相關" %}
* [syntax](../../../err/syntax "mention") ⟩ [unexpected-token](../../../err/syntax/unexpected-token "mention")
{% endtab %}
{% endtabs %}
