Error Reference

JavaScript SyntaxError: unexpected token in script

JavaScript SyntaxError: unexpected token in script

Where this appears

This error appears in the browser console when executing client scripts, UI policies, or catalog client scripts. It also shows in the System Log under System Diagnostics > Log > Application Log when server-side scripts contain syntax errors. The error prevents the script from being parsed and executed by the JavaScript engine.

Sourdough
Chrome Extension

Debugging this in a real instance?

Sourdough lets you trace "JavaScript SyntaxError: unexpected token in script" through system logs, script errors, and API health in the ServiceNow tab you are already using.

Add to Chrome

Free to install. Pro $5/month after a 14-day no-card trial.
Pro requires the ServiceNow admin role. Upgrade inside the extension.

Overview
Tasks
CMDB
API
Metrics
Monitor
Internals
Instance:sourdoughdev·Version:Yokohama
Instance StateONLINE
System StatusFully Operational
Session Timeout90 minutes
Logged-In Sessions2 (20 active)
Build Nameyokohama-12-18-2024_p1
IP Address10.159.128.43
Instance HealthHealth Score: 90%
🔥 5dSourdough (Chrome Plugin)Dark Mode

Cause

Missing or mismatched braces, brackets, or parentheses in JavaScript code blocks, particularly in if statements, functions, or object definitions. (high likelihood)

Using ES6+ arrow function syntax (=>) in client scripts or catalog client scripts which run in older JavaScript engines that only support ES5. (high likelihood)

Template literals using backticks (`) instead of single or double quotes in environments that don't support ES6 template literals. (medium likelihood)

Incorrect use of reserved JavaScript keywords as variable names or function names, such as 'class', 'const', or 'let' in ES5 contexts. (medium likelihood)

Malformed regular expressions or string literals with unescaped special characters or incorrect quotation mark usage. (low likelihood)

Fix

Step 1: Navigate to System Diagnostics > Log > Application Log to find the exact line number and script location where the syntax error occurred.

Step 2: Open the problematic script record (Business Rule, Client Script, UI Policy, etc.) and locate the line mentioned in the error message.

Step 3: Check for missing or mismatched braces, brackets, and parentheses by counting opening and closing pairs in the script.

Step 4: Replace any ES6+ syntax like arrow functions (=>) with traditional function declarations if the script runs client-side or in older environments.

Step 5: Convert template literals (backticks) to string concatenation using single or double quotes if ES6 is not supported in the execution context.

Step 6: Validate that all string literals are properly quoted and escape any special characters that might break the syntax.

Step 7: Use the Script Debugger or browser developer tools to test the corrected syntax before saving the script record.

Step 8: Clear the browser cache and test the functionality to ensure the syntax error is resolved and the script executes properly.

Test Your Knowledge

Quick 3-question quiz. See how your ServiceNow skills stack up.

Question 1 of 3Performance

A list view on a table with millions of records is slow. Best fix?

Select an answer to continue