大网数据广告活动
Web 标准与前端开发

SyntaxError: octal escape sequences can't be used in untagged template literals or in strict mode code

· 52CE 我爱测

The JavaScript exception "octal escape sequences can't be used in untagged template literals or in strict mode code" occurs when octal escape sequences are used in strict mode string literals or untagged template literals.

The JavaScript exception "octal escape sequences can't be used in untagged template literals or in strict mode code" occurs when octal escape sequences are used in strict mode string literals or untagged template literals.

Message

SyntaxError: Octal escape sequences are not allowed in strict mode. (V8-based)
SyntaxError: \8 and \9 are not allowed in strict mode. (V8-based)
SyntaxError: Octal escape sequences are not allowed in template strings. (V8-based)
SyntaxError: \8 and \9 are not allowed in template strings. (V8-based)
SyntaxError: octal escape sequences can't be used in untagged template literals or in strict mode code (Firefox)
SyntaxError: the escapes \8 and \9 can't be used in untagged template literals or in strict mode code (Firefox)
SyntaxError: The only valid numeric escape in strict mode is '\0' (Safari)

Error type

SyntaxError

What went wrong?

The string escape sequence of the form \ followed by any number of digits, except a single 0, is deprecated. If you want to represent a character by its code point value, you should use the \x or \u escape sequence instead, such as \x01 or \u0001 instead of \1.

Untagged template literals are never allowed to contain octal escape sequences, whether in strict mode or not. However, tagged template literals can contain any form of escape sequence, and will cause the template array received by the tag function to contain undefined.

Examples

Octal escape sequences

"use strict";

"\251";

// SyntaxError: octal escape sequences can't be used in untagged template literals or in strict mode code

Valid octal numbers

For octal escape sequences, you can use hexadecimal escape sequences instead:

"\xA9";

If you want to represent some source text literally without interpreting any escape sequence, use String.raw:

String.raw`\251`; // A string containing four characters

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。