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)
- SyntaxError: Unexpected token – MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token - JSON Syntax Rules
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON