SyntaxError: Unexpected token-The 3 things beginners should check first when this error appears in Google Apps Script

* If you need help with the content of this article for work or development, individual support is available.

When working with Google Apps Script, you may suddenly see this message:

SyntaxError: Unexpected token

It looks scary, but the good news is:
This error usually happens only when there is a small typo or unwanted character in your code.

Here are the three simplest places to check, especially for beginners.


■ Top 3 things to check

① Missing or extra commas / brackets

This is the most common cause.

  • Missing ,
  • Missing } or )
  • Extra commas

Example:

const data = {
  name: "yama"
  age: 30   // ← Missing comma → Unexpected token
};

② A string is not properly closed

A missing ', ", or ` will break the entire line.

const msg = `Hello ${name};  // ← Missing backtick

③ Mixing JSON syntax with JavaScript object syntax

This happens often when copying API examples.

const payload = {
  "name": "test",
  age: 30,  // ← Mixed styles cause errors
};

JSON requires quotes on keys and disallows trailing commas,
while JavaScript objects do not.


■ How beginners can quickly find and fix the issue

Step 1: Check the indicated line AND the lines above it

The actual cause is often 1–2 lines earlier.

Step 2: Use the “Format code” button

If formatting breaks or stops in the middle,
the broken part is exactly where the error is.

Step 3: Review code you copied from the web

API responses (LINE, Slack, Google API, etc.) often contain JSON,
which cannot be pasted directly into GAS without editing.


■ Real beginner example

● Copying LINE API JSON directly into GAS

"messages": [
  {
    "type": "text",
    "text": "Hello"
  }
]

Correct GAS code:

messages: [
  {
    type: "text",
    text: "Hello"
  }
]

■ Summary

SyntaxError: Unexpected token is almost never a complex problem.
Beginners can fix it by checking:

  • commas
  • brackets
  • string closures
  • JSON vs JavaScript syntax

These simple points solve most cases instantly.


Reference URLs (English)

  1. SyntaxError: Unexpected token – MDN
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token
  2. JSON Syntax Rules
    https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON

ZIDOOKA!

Need help with the content of this article?

I provide individual technical support related to the issues described in this article, as a freelance developer. If the problem is blocking your work or internal tasks, feel free to reach out.

Support starts from $30 USD (Estimate provided in advance)
Thank you for reading

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Policy on AI Usage

Some articles on this site are written with the assistance of AI. However, we do not rely entirely on AI for writing; it is used strictly as a support tool.