diff --git a/files/en-us/_redirects.txt b/files/en-us/_redirects.txt index 7e757e9704cd2f9..20cf7f794226e7e 100644 --- a/files/en-us/_redirects.txt +++ b/files/en-us/_redirects.txt @@ -12508,7 +12508,9 @@ /en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal /en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal_literal /en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_toLocaleFormat /en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features /en-US/docs/Web/JavaScript/Reference/Errors/Malformed_formal_parameter /en-US/docs/Web/JavaScript/Reference/Errors/Missing_formal_parameter +/en-US/docs/Web/JavaScript/Reference/Errors/Missing_semicolon_before_statement /en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token /en-US/docs/Web/JavaScript/Reference/Errors/Not_a_codepoint /en-US/docs/Web/JavaScript/Reference/Errors/Not_a_valid_code_point +/en-US/docs/Web/JavaScript/Reference/Errors/Unterminated_string_literal /en-US/docs/Web/JavaScript/Reference/Errors/String_literal_EOL /en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope /en-US/docs/Web/JavaScript/Reference/Functions /en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode /en-US/docs/Web/JavaScript/Reference/Strict_mode /en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode/Transitioning_to_strict_mode /en-US/docs/Web/JavaScript/Reference/Strict_mode diff --git a/files/en-us/_wikihistory.json b/files/en-us/_wikihistory.json index e0179807b62bba2..6bd8b8c3eb99cd7 100644 --- a/files/en-us/_wikihistory.json +++ b/files/en-us/_wikihistory.json @@ -103945,10 +103945,6 @@ "modified": "2020-11-30T05:50:18.195Z", "contributors": ["mfuji09", "fscholz"] }, - "Web/JavaScript/Reference/Errors/Equal_as_assign": { - "modified": "2020-04-16T08:23:11.906Z", - "contributors": ["fscholz", "arai"] - }, "Web/JavaScript/Reference/Errors/Getter_only": { "modified": "2020-05-06T10:16:12.614Z", "contributors": ["fscholz", "PatrickKettner"] @@ -104051,21 +104047,6 @@ "modified": "2020-05-06T09:40:23.426Z", "contributors": ["fscholz", "PatrickKettner"] }, - "Web/JavaScript/Reference/Errors/Missing_semicolon_before_statement": { - "modified": "2020-04-16T07:49:53.557Z", - "contributors": [ - "fscholz", - "sideshowbarker", - "kevinbmagill", - "SphinxKnight", - "ogigpermana", - "luisvallejomohl", - "PatrickKettner", - "nmve", - "jwhitlock", - "loveabushu1" - ] - }, "Web/JavaScript/Reference/Errors/More_arguments_needed": { "modified": "2020-05-06T09:46:25.163Z", "contributors": ["fscholz", "Bzbarsky", "PatrickKettner"] @@ -104200,6 +104181,10 @@ "modified": "2020-04-15T11:09:10.798Z", "contributors": ["fscholz", "PatrickKettner", "nmve", "nbp", "arai"] }, + "Web/JavaScript/Reference/Errors/String_literal_EOL": { + "modified": "2020-04-16T08:26:04.965Z", + "contributors": ["fscholz", "PatrickKettner", "neroshan12", "nmve"] + }, "Web/JavaScript/Reference/Errors/Too_much_recursion": { "modified": "2020-04-14T12:37:45.784Z", "contributors": [ @@ -104216,10 +104201,6 @@ "modified": "2020-06-04T08:11:57.239Z", "contributors": ["Sheppy", "fscholz", "ZavodPodushek", "nmve"] }, - "Web/JavaScript/Reference/Errors/Undefined_prop": { - "modified": "2020-10-24T03:39:49.210Z", - "contributors": ["angleKH", "fscholz", "arai", "fiejen", "nmve", "Sheppy"] - }, "Web/JavaScript/Reference/Errors/Unexpected_token": { "modified": "2020-04-15T11:24:29.836Z", "contributors": [ @@ -104252,10 +104233,6 @@ "modified": "2020-04-16T07:01:37.303Z", "contributors": ["fscholz", "wbamberg", "PatrickKettner"] }, - "Web/JavaScript/Reference/Errors/Unterminated_string_literal": { - "modified": "2020-04-16T08:26:04.965Z", - "contributors": ["fscholz", "PatrickKettner", "neroshan12", "nmve"] - }, "Web/JavaScript/Reference/Errors/in_operator_no_object": { "modified": "2020-12-06T05:37:14.314Z", "contributors": [ diff --git a/files/en-us/learn/javascript/first_steps/what_went_wrong/index.md b/files/en-us/learn/javascript/first_steps/what_went_wrong/index.md index 457c722a115c379..30ef8f7f90059a2 100644 --- a/files/en-us/learn/javascript/first_steps/what_went_wrong/index.md +++ b/files/en-us/learn/javascript/first_steps/what_went_wrong/index.md @@ -194,24 +194,6 @@ Try updating both lines like this, then save and refresh — the game should now There are other common errors you'll come across in your code. This section highlights most of them. -### SyntaxError: missing ; before statement - -This error generally means that you have missed a semicolon at the end of one of your lines of code, but it can sometimes be more cryptic. For example, if we change this line inside the `checkGuess()` function: - -```js -const userGuess = Number(guessField.value); -``` - -to - -```js example-bad -const userGuess === Number(guessField.value); -``` - -It throws this error because it thinks you are trying to do something different. You should make sure that you don't mix up the assignment operator (`=`) — which sets a variable to be equal to a value — with the strict equality operator (`===`), which tests whether one value is equal to another, and returns a `true`/`false` result. - -> **Note:** See our [SyntaxError: missing ; before statement](/en-US/docs/Web/JavaScript/Reference/Errors/Missing_semicolon_before_statement) reference page for more details about this error. - ### The program always says you've won, regardless of the guess you enter This could be another symptom of mixing up the assignment and strict equality operators. For example, if we were to change this line inside `checkGuess()`: @@ -254,13 +236,13 @@ This has caused the browser to think that we are trying to pass the contents of This is easy — it generally means that you've missed one of your curly braces from a function or conditional structure. We got this error by deleting one of the closing curly braces near the bottom of the `checkGuess()` function. -### SyntaxError: expected expression, got '_string_' or SyntaxError: unterminated string literal +### SyntaxError: expected expression, got '_string_' or SyntaxError: string literal contains an unescaped line break These errors generally mean that you've left off a string value's opening or closing quote mark. In the first error above, _string_ would be replaced with the unexpected character(s) that the browser found instead of a quote mark at the start of a string. The second error means that the string has not been ended with a quote mark. For all of these errors, think about how we tackled the examples we looked at in the walkthrough. When an error arises, look at the line number you are given, go to that line and see if you can spot what's wrong. Bear in mind that the error is not necessarily going to be on that line, and also that the error might not be caused by the exact same problem we cited above! -> **Note:** See our [SyntaxError: Unexpected token](/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token) and [SyntaxError: unterminated string literal](/en-US/docs/Web/JavaScript/Reference/Errors/Unterminated_string_literal) reference pages for more details about these errors. +> **Note:** See our [SyntaxError: Unexpected token](/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token) and [SyntaxError: string literal contains an unescaped line break](/en-US/docs/Web/JavaScript/Reference/Errors/String_literal_EOL) reference pages for more details about these errors. ## Summary diff --git a/files/en-us/web/javascript/reference/errors/equal_as_assign/index.md b/files/en-us/web/javascript/reference/errors/equal_as_assign/index.md deleted file mode 100644 index 0f698a220cbc20a..000000000000000 --- a/files/en-us/web/javascript/reference/errors/equal_as_assign/index.md +++ /dev/null @@ -1,66 +0,0 @@ ---- -title: "SyntaxError: test for equality (==) mistyped as assignment (=)?" -slug: Web/JavaScript/Reference/Errors/Equal_as_assign -page-type: javascript-error ---- - -{{jsSidebar("Errors")}} - -The JavaScript warning "test for equality (==) mistyped as assignment (=)?" occurs when -there was an assignment (`=`) when you would normally expect a test for -equality (`==`). - -## Message - -```plain -Warning: SyntaxError: test for equality (==) mistyped as assignment (=)? -``` - -## Error type - -(Firefox only) {{jsxref("SyntaxError")}} warning which is reported only if -`javascript.options.strict` preference is set to `true`. - -## What went wrong? - -There was an assignment (`=`) when you would normally expect a test for -equality (`==`). To help debugging, JavaScript (with strict warnings enabled) -warns about this pattern. - -## Examples - -### Assignment within conditional expressions - -It is advisable to not use simple assignments in a conditional expression (such as -[`if...else`](/en-US/docs/Web/JavaScript/Reference/Statements/if...else)), -because the assignment can be confused with equality when glancing over the code. For -example, do not use the following code: - -```js-nolint example-bad -if (x = y) { - // do the right thing -} -``` - -If you need to use an assignment in a conditional expression, a common practice is to -put additional parentheses around the assignment. For example: - -```js -if ((x = y)) { - // do the right thing -} -``` - -Otherwise, you probably meant to use a comparison operator (e.g. `==` or -`===`): - -```js -if (x === y) { - // do the right thing -} -``` - -## See also - -- [`if...else`](/en-US/docs/Web/JavaScript/Reference/Statements/if...else) -- [Equality operators](/en-US/docs/Web/JavaScript/Reference/Operators#equality_operators) diff --git a/files/en-us/web/javascript/reference/errors/illegal_character/index.md b/files/en-us/web/javascript/reference/errors/illegal_character/index.md index cb3bd0bb5c040ff..8f52ae4060ae8f2 100644 --- a/files/en-us/web/javascript/reference/errors/illegal_character/index.md +++ b/files/en-us/web/javascript/reference/errors/illegal_character/index.md @@ -6,15 +6,14 @@ page-type: javascript-error {{jsSidebar("Errors")}} -The JavaScript exception "illegal character" occurs when there is an invalid or -unexpected token that doesn't belong at this position in the code. +The JavaScript exception "illegal character" occurs when the [lexer](/en-US/docs/Web/JavaScript/Reference/Lexical_grammar) reads a character that's not part of a string literal, and the character cannot constitute a valid token in the language. ## Message ```plain -SyntaxError: Invalid character (Edge) -SyntaxError: illegal character (Firefox) -SyntaxError: Invalid or unexpected token (Chrome) +SyntaxError: Invalid or unexpected token (V8-based) +SyntaxError: illegal character U+201C (Firefox) +SyntaxError: Invalid character '\u201c' (Safari) ``` ## Error type @@ -23,8 +22,7 @@ SyntaxError: Invalid or unexpected token (Chrome) ## What went wrong? -There is an invalid or unexpected token that doesn't belong at this position in the -code. Use an editor that supports syntax highlighting and carefully check your code +There is an invalid character that the interpreter doesn't understand. You should either put it in a string literal or replace it with another character. Use an editor that supports syntax highlighting and carefully check your code against mismatches like a minus sign (`-`) versus a dash (`–`) or simple quotes (`"`) versus non-standard quotation marks (`"`). @@ -62,14 +60,14 @@ Some editors and IDEs will notify you or at least use a slightly different highl It's easy to forget a character here or there. ```js-nolint example-bad -const colors = ["#000", #333", "#666"]; -// SyntaxError: illegal character +const operators = ["+", "-", ×", "÷"]; +// SyntaxError: illegal character U+00D7 ``` -Add the missing quote for `"#333"`. +Add the missing quote for `"×"`. ```js example-good -const colors = ["#000", "#333", "#666"]; +const operators = ["+", "-", "×", "÷"]; ``` ### Hidden characters diff --git a/files/en-us/web/javascript/reference/errors/missing_semicolon_before_statement/index.md b/files/en-us/web/javascript/reference/errors/missing_semicolon_before_statement/index.md deleted file mode 100644 index 308603094678a2c..000000000000000 --- a/files/en-us/web/javascript/reference/errors/missing_semicolon_before_statement/index.md +++ /dev/null @@ -1,102 +0,0 @@ ---- -title: "SyntaxError: missing ; before statement" -slug: Web/JavaScript/Reference/Errors/Missing_semicolon_before_statement -page-type: javascript-error ---- - -{{jsSidebar("Errors")}} - -The JavaScript exception "missing ; before statement" occurs when there is a semicolon (`;`) -missing somewhere and can't be added -by [automatic semicolon insertion (ASI)](/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion). -You need to provide a semicolon, so that JavaScript can parse the source code correctly. - -## Message - -```plain -SyntaxError: Expected ';' (Edge) -SyntaxError: missing ; before statement (Firefox) -``` - -## Error type - -{{jsxref("SyntaxError")}}. - -## What went wrong? - -There is a semicolon (`;`) missing somewhere. [JavaScript statements](/en-US/docs/Web/JavaScript/Reference/Statements) must -be terminated with semicolons. Some of them are affected -by [automatic semicolon insertion (ASI)](/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion), -but in this case you need to provide a semicolon, -so that JavaScript can parse the source code correctly. - -However, oftentimes, this error is only a consequence of another error, like not -escaping strings properly, or using `var` wrongly. You might also have too -many parenthesis somewhere. Carefully check the syntax when this error is thrown. - -## Examples - -### Unescaped strings - -This error can occur easily when not escaping strings properly and the JavaScript -engine is expecting the end of your string already. For example: - -```js-nolint example-bad -const foo = 'Tom's bar'; -// SyntaxError: missing ; before statement -``` - -You can use double quotes, or escape the apostrophe: - -```js-nolint example-good -const foo = "Tom's bar"; -// OR -const foo = 'Tom\'s bar'; -``` - -### Declaring properties with keyword - -You **cannot** declare properties of an object or array with a -`let`, `const`, or `var` declaration. - -```js-nolint example-bad -const obj = {}; -const obj.foo = "hi"; // SyntaxError missing ; before statement - -const array = []; -const array[0] = "there"; // SyntaxError missing ; before statement -``` - -Instead, omit the keyword: - -```js example-good -const obj = {}; -obj.foo = "hi"; - -const array = []; -array[0] = "there"; -``` - -### Bad keywords - -If you come from another programming language, it is also common to use keywords that -don't mean the same or have no meaning at all in JavaScript: - -```js-nolint example-bad -def print(info) { - console.log(info); -} // SyntaxError missing ; before statement -``` - -Instead, use `function` instead of `def`: - -```js example-good -function print(info) { - console.log(info); -} -``` - -## See also - -- [Automatic semicolon insertion (ASI)](/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion) -- [Statements and declarations](/en-US/docs/Web/JavaScript/Reference/Statements) diff --git a/files/en-us/web/javascript/reference/errors/unterminated_string_literal/index.md b/files/en-us/web/javascript/reference/errors/string_literal_eol/index.md similarity index 84% rename from files/en-us/web/javascript/reference/errors/unterminated_string_literal/index.md rename to files/en-us/web/javascript/reference/errors/string_literal_eol/index.md index 4b094b47f59d66c..5e0db3b3f2e7b80 100644 --- a/files/en-us/web/javascript/reference/errors/unterminated_string_literal/index.md +++ b/files/en-us/web/javascript/reference/errors/string_literal_eol/index.md @@ -1,20 +1,21 @@ --- -title: "SyntaxError: unterminated string literal" -slug: Web/JavaScript/Reference/Errors/Unterminated_string_literal +title: "SyntaxError: string literal contains an unescaped line break" +slug: Web/JavaScript/Reference/Errors/String_literal_EOL page-type: javascript-error --- {{jsSidebar("Errors")}} -The JavaScript error "unterminated string literal" occurs when there is an unterminated +The JavaScript error "string literal contains an unescaped line break" occurs when there is an unterminated [string literal](/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#string_literals) somewhere. String literals must be enclosed by single -(`'`) or double (`"`) quotes. +(`'`) or double (`"`) quotes and cannot split across multiple lines. ## Message ```plain -SyntaxError: Unterminated string constant (Edge) -SyntaxError: unterminated string literal (Firefox) +SyntaxError: Invalid or unexpected token (V8-based) +SyntaxError: '' string literal contains an unescaped line break (Firefox) +SyntaxError: Unexpected EOF (Safari) ``` ## Error type diff --git a/files/en-us/web/javascript/reference/errors/undefined_prop/index.md b/files/en-us/web/javascript/reference/errors/undefined_prop/index.md deleted file mode 100644 index 52ed97fb5c08d26..000000000000000 --- a/files/en-us/web/javascript/reference/errors/undefined_prop/index.md +++ /dev/null @@ -1,64 +0,0 @@ ---- -title: 'ReferenceError: reference to undefined property "x"' -slug: Web/JavaScript/Reference/Errors/Undefined_prop -page-type: javascript-error ---- - -{{jsSidebar("Errors")}} - -The JavaScript warning "reference to undefined property" occurs when a script attempted -to access an object property which doesn't exist. - -## Message - -```plain -ReferenceError: reference to undefined property "x" (Firefox) -``` - -## Error type - -(Firefox only) {{jsxref("ReferenceError")}} warning which is reported only if -`javascript.options.strict` preference is set to `true`. - -## What went wrong? - -The script attempted to access an object property which doesn't exist. There are two -ways to access properties; see the [property accessors](/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors) reference page to learn more about them. - -## Examples - -### Invalid cases - -In this case, the property `bar` is an undefined property, so a -`ReferenceError` will occur. - -```js example-bad -const foo = {}; -foo.bar; // ReferenceError: reference to undefined property "bar" -``` - -### Valid cases - -To avoid the error, you need to either add a definition for `bar` to the -object or check for the existence of the `bar` property before trying to -access it; ways to do that include using the {{jsxref("Operators/in", "in")}} operator, -or the {{jsxref("Object.hasOwn()")}} method, like this: - -```js example-good -const foo = {}; - -// Define the bar property - -foo.bar = "moon"; -console.log(foo.bar); // "moon" - -// Test to be sure bar exists before accessing it - -if (Object.hasOwn(foo, "bar")) { - console.log(foo.bar); -} -``` - -## See also - -- [Property accessors](/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors) diff --git a/files/en-us/web/javascript/reference/errors/unexpected_token/index.md b/files/en-us/web/javascript/reference/errors/unexpected_token/index.md index 2e7d14bfc84e180..20bc5962be2a19f 100644 --- a/files/en-us/web/javascript/reference/errors/unexpected_token/index.md +++ b/files/en-us/web/javascript/reference/errors/unexpected_token/index.md @@ -6,18 +6,25 @@ page-type: javascript-error {{jsSidebar("Errors")}} -The JavaScript exceptions "unexpected token" occur when a specific language construct -was expected, but something else was provided. This might be a simple typo. +The JavaScript exceptions "unexpected token" occur when the parser does not see a token it recognizes at the given position, so it cannot make sense of the structure of the program. This might be a simple typo. ## Message ```plain -SyntaxError: expected expression, got "x" -SyntaxError: expected property name, got "x" -SyntaxError: expected target, got "x" -SyntaxError: expected rest argument name, got "x" -SyntaxError: expected closing parenthesis, got "x" -SyntaxError: expected '=>' after argument list, got "x" +SyntaxError: Unexpected token ';' (V8-based) +SyntaxError: Unexpected identifier 'x' (V8-based) +SyntaxError: Unexpected number (V8-based) +SyntaxError: Unexpected string (V8-based) +SyntaxError: Unexpected regular expression (V8-based) +SyntaxError: Unexpected template string (V8-based) + +SyntaxError: unexpected token: identifier (Firefox) +SyntaxError: expected expression, got "x" (Firefox) +SyntaxError: expected property name, got "x" (Firefox) +SyntaxError: expected target, got "x" (Firefox) +SyntaxError: expected meta, got "x" (Firefox) +SyntaxError: expected rest argument name, got "x" (Firefox) +SyntaxError: expected closing parenthesis, got "x" (Firefox) ``` ## Error type