Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

SyntaxError: Unexpected '#' used outside of class body

The JavaScript exception "Unexpected '#' used outside of class body" occurs when a hash("#") is encountered in an unexpected context, most notablyoutside of a class declaration.Hashes are valid at the beginning of a file as ahashbang comment,or inside of a class as part of a private field. You may encounter this error if you forgetthe quotation marks when trying to access a DOM identifier as well.

Message

SyntaxError: Unexpected '#' used outside of class body.

Error type

What went wrong?

We encountered a# somewhere unexpected. This may be due to code moving around and nolonger being part of a class, a hashbang comment found on a line other than the firstline of a file, or accidentally forgetting the quotation marks around a DOM identifier.

Examples

Missing quotation marks

For each case, there might be something slightly wrong. For example

js
document.querySelector(#some-element)

This can be fixed via

js
document.querySelector("#some-element");

Outside of a class

js
class ClassWithPrivateField {  #privateField;  constructor() {}}this.#privateField = 42;

This can be fixed by moving the private field back into the class

js
class ClassWithPrivateField {  #privateField;  constructor() {    this.#privateField = 42;  }}

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp