SyntaxError: Unexpected token – Causes and Fixes [GAS / JavaScript]

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

When writing JavaScript or Google Apps Script (GAS), you may encounter an error like this:

SyntaxError: Unexpected token

This article explains why this error occurs and how to fix it, with practical patterns that developers commonly encounter. We’ll focus on the pain points where GAS and JavaScript beginners often stumble, with solutions to prevent recurrence.

What Does SyntaxError: Unexpected token Mean?

In short, this error means:

“A grammatically unexpected symbol or character was found.”

JavaScript’s parser checks the syntax before executing code. When an unexpected symbol (token) appears, this error occurs.

SyntaxError: Unexpected token '{'

It often shows which specific symbol is problematic.

Common Cause #1: Forgot to Close Brackets or Parentheses

This is one of the most frequent causes.

Example: Missing closing brace

function test() {
  Logger.log("test");

Missing }.

Example: Unclosed parenthesis

Logger.log("Hello"

Missing ).

How to Fix It

  • Use your editor’s syntax highlighting
  • Use bracket-matching features
  • Get in the habit of closing brackets immediately
function test() {
  Logger.log("test");
}

Common Cause #2: Comma or Semicolon Mistakes

Example: Extra comma at the end of an array

const list = [1, 2, 3,];

The trailing comma can cause problems in some environments (older browsers or certain GAS contexts).

Example: Missing comma between object properties

const user = {
  name: "Taro"
  age: 20
};

A comma is needed after name.

How to Fix It

  • Check delimiters in objects and arrays
  • Don’t forget commas
const user = {
  name: "Taro",
  age: 20
};

Common Cause #3: Using Reserved Words as Variable Names

JavaScript has reserved words that cannot be used.

Example: Using reserved words as variable names

const function = "test";
const class = "myClass";

function and class are reserved words.

How to Fix It

  • Avoid reserved words
  • Use alternatives like func or className
const func = "test";
const className = "myClass";

Common Cause #4: Unclosed String Quotes

Example: String ending prematurely

const message = "Hello World;
Logger.log(message);

Missing closing ".

How to Fix It

  • Always close strings with matching quotes
  • Use editor autocomplete
const message = "Hello World";
Logger.log(message);

Common Cause #5: Comment Syntax Errors

Example: Unclosed multi-line comment

/* This is a comment
Logger.log("test");

Needs to be closed with */.

How to Fix It

  • Write comments with correct syntax
  • Multi-line comments: /* ... */
  • Single-line comments: //
/* This is a comment */
Logger.log("test");

Common Cause #6: JSON Syntax Errors

GAS often handles JSON, where syntax errors are common.

Example: JSON keys without quotes

const data = {
  name: "Taro"
};

This is a JavaScript object, not JSON. In JSON:

const jsonString = '{"name": "Taro"}';

How to Fix It

  • When using JSON.parse(), write proper JSON format
  • Quote keys as strings
const data = JSON.parse('{"name": "Taro"}');

Basic Debugging Checklist

When you encounter SyntaxError: Unexpected token, check these points in order:

  1. Are all brackets and parentheses properly closed?
  2. Are commas and semicolons correctly placed?
  3. Are you using reserved words as variable names?
  4. Are string quotes closed?
  5. Is comment syntax correct?
  6. Is JSON syntax correct?

These six points solve most cases.

Best Practices to Prevent This Error

// Use editor syntax highlighting
// Use auto-formatting features
// Close brackets immediately
function test() {
  // Write code here
}
// Don't forget commas
const user = {
  name: "Taro",
  age: 20
};

Summary

SyntaxError: Unexpected token means “there’s a grammatically incorrect symbol.”

In GAS especially, just being aware of:

  • Matching brackets and parentheses
  • JSON syntax
  • String quotes

can drastically reduce how often this error occurs.

The error message shows “which symbol is problematic,” so carefully checking the code around that symbol is the quickest path to a solution.

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.