Categories
JavaScript Answers

How to Fix the ‘SyntaxError: JSON.parse: bad parsing ‘ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: JSON.parse: bad parsing’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: JSON.parse: bad parsing’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: JSON.parse: bad parsing’ When Developing JavaScript Apps

To fix the ‘SyntaxError: JSON.parse: bad parsing’ when we’re developing JavaScript apps, we should make sure we pass in a valid JSON string as an argument of the JSON.parse method.

Other possible error messages for various JSON parse errors include:

SyntaxError: JSON.parse: unterminated string literal
SyntaxError: JSON.parse: bad control character in string literal
SyntaxError: JSON.parse: bad character in string literal
SyntaxError: JSON.parse: bad Unicode escape
SyntaxError: JSON.parse: bad escape character
SyntaxError: JSON.parse: unterminated string
SyntaxError: JSON.parse: no number after minus sign
SyntaxError: JSON.parse: unexpected non-digit
SyntaxError: JSON.parse: missing digits after decimal point
SyntaxError: JSON.parse: unterminated fractional number
SyntaxError: JSON.parse: missing digits after exponent indicator
SyntaxError: JSON.parse: missing digits after exponent sign
SyntaxError: JSON.parse: exponent part is missing a number
SyntaxError: JSON.parse: unexpected end of data
SyntaxError: JSON.parse: unexpected keyword
SyntaxError: JSON.parse: unexpected character
SyntaxError: JSON.parse: end of data while reading object contents
SyntaxError: JSON.parse: expected property name or '}'
SyntaxError: JSON.parse: end of data when ',' or ']' was expected
SyntaxError: JSON.parse: expected ',' or ']' after array element
SyntaxError: JSON.parse: end of data when property name was expected
SyntaxError: JSON.parse: expected double-quoted property name
SyntaxError: JSON.parse: end of data after property name when ':' was expected
SyntaxError: JSON.parse: expected ':' after property name in object
SyntaxError: JSON.parse: end of data after property value in object
SyntaxError: JSON.parse: expected ',' or '}' after property value in object
SyntaxError: JSON.parse: expected ',' or '}' after property-value pair in object literal
SyntaxError: JSON.parse: property names must be double-quoted strings
SyntaxError: JSON.parse: expected property name or '}'
SyntaxError: JSON.parse: unexpected character
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
SyntaxError: JSON.parse Error: Invalid character at position {0} (Edge)

For instance, we shouldn’t pass in a JSON string that has trailing commas:

JSON.parse('[1, 2, 3, 4,]');
JSON.parse('{"foo": 1,}');

The first line has a trailing comma at the end of the array.

The 2nd has a trailing commas at the end of the object.

Instead, we should fix the error by removing them:

JSON.parse('[1, 2, 3, 4]');
JSON.parse('{"foo": 1}');

Also, property names must be double quoted strings, so instead of writing:

JSON.parse("{'foo': 1}");

We write:

JSON.parse('{"foo": 1}');

Also, we can’t have numbers with leading zeroes in our JSON string:

JSON.parse('{"foo": 01}');

Instead, we fix that by removing the leading zero:

JSON.parse('{"foo": 1}');

Trailing decimal points are also invalid JSON, so we can’t write:

JSON.parse('{"foo": 1.}');

Instead, we write:

JSON.parse('{"foo": 1.0}');

Conclusion

To fix the ‘SyntaxError: JSON.parse: bad parsing’ when we’re developing JavaScript apps, we should make sure we pass in a valid JSON string as an argument of the JSON.parse method.

Categories
JavaScript Answers

How to Fix the ‘SyntaxError: “x” is a reserved identifier ‘ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: "x" is a reserved identifier’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: "x" is a reserved identifier’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: "x" is a reserved identifier’ When Developing JavaScript Apps

To fix the ‘SyntaxError: "x" is a reserved identifier’ when we’re developing JavaScript apps, we should make sure we aren’t trying to use reserved keywords for variable names.

In Edge, the error message for this error is SyntaxError: The use of a future reserved word for an identifier is invalid.

In Firefox, the error message for this error is SyntaxError: "x" is a reserved identifier.

And in Chrome, the error message for this error is SyntaxError: Unexpected reserved word.

enum is a reserved keyword in strict or sloppy mode.

The following keywords are reserved keywords in strict mode only:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static

For instance, we shouldn’t write code like"

const enum = { RED: 0, GREEN: 1, BLUE: 2 };

Also, we shouldn’t write coee like:

"use strict";
const package = ["potatoes", "apples", "fries"];

Instead, we should rename them so their names aren’t in the reserved keywords list:

const colorEnum = { RED: 0, GREEN: 1, BLUE: 2 };
const list = ["potatoes", "rice", "fries"];

Conclusion

To fix the ‘SyntaxError: "x" is a reserved identifier’ when we’re developing JavaScript apps, we should make sure we aren’t trying to use reserved keywords for variable names.

In Edge, the error message for this error is SyntaxError: The use of a future reserved word for an identifier is invalid.

In Firefox, the error message for this error is SyntaxError: "x" is a reserved identifier.

And in Chrome, the error message for this error is SyntaxError: Unexpected reserved word.

Categories
JavaScript Answers

How to Fix the ‘SyntaxError: “use strict” not allowed in function with non-simple parameters ‘ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: "use strict" not allowed in function with non-simple parameters’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: "use strict" not allowed in function with non-simple parameters’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: "use strict" not allowed in function with non-simple parameters’ When Developing JavaScript Apps

To fix the ‘SyntaxError: "use strict" not allowed in function with non-simple parameters’ when we’re developing JavaScript apps, we should make sure we don’t add the 'use strict' directive inside a function that uses default parameters, rest parameters, or destructuring parameters.

In Edge, the error message for this error is Cannot apply strict mode on functions with non-simple parameter list .

In Firefox, the error message for this error is SyntaxError: "use strict" not allowed in function with default parameter, SyntaxError: "use strict" not allowed in function with rest parameter, and SyntaxError: "use strict" not allowed in function with destructuring parameter.

And in Chrome, the error message for this error is SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list.

For instance, we shouldn’t write code like:

const sum = (a = 1, b = 2) => {
  'use strict';
  return a + b;
}

Instead, we should put 'use strict' before the function:

"use strict";
const sum = (a = 1, b = 2) => {
  return a + b;
};

Conclusion

To fix the ‘SyntaxError: "use strict" not allowed in function with non-simple parameters’ when we’re developing JavaScript apps, we should make sure we don’t add the 'use strict' directive inside a function that uses default parameters, rest parameters, or destructuring parameters.

In Edge, the error message for this error is Cannot apply strict mode on functions with non-simple parameter list .

In Firefox, the error message for this error is SyntaxError: "use strict" not allowed in function with default parameter, SyntaxError: "use strict" not allowed in function with rest parameter, and SyntaxError: "use strict" not allowed in function with destructuring parameter.

And in Chrome, the error message for this error is SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list.

Categories
JavaScript Answers

How to Fix the ‘SyntaxError: “0”-prefixed octal literals and octal escape seq. are deprecated’ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated’ When Developing JavaScript Apps

To fix the ‘SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated’ when we’re developing JavaScript apps, we should make sure that we don’t use 0-prefixed numbers to add octal numbers in our JavaScript code.

For instance, we shouldn’t write code like:

03;

in our code.

Instead, we write:

0o3;

to add an octal number.

We should also use hex escape sequences instead of octal escape sequences:

'\xA9';

Conclusion

To fix the ‘SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated’ when we’re developing JavaScript apps, we should make sure that we don’t use 0-prefixed numbers to add octal numbers in our JavaScript code.

We should also use hex escape sequences instead of octal escape sequences.

Categories
JavaScript Answers

How to Fix the ‘ReferenceError: invalid assignment left-hand side’ Error in Our JavaScript App?

Sometimes, we may run into the ‘ReferenceError: invalid assignment left-hand side’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘ReferenceError: invalid assignment left-hand side’ when we’re developing JavaScript apps.

Fix the ‘ReferenceError: invalid assignment left-hand side’ When Developing JavaScript Apps

To fix the ‘ReferenceError: invalid assignment left-hand side’ when we’re developing JavaScript apps, we should make sure that we’re accidentally using the = operator for doing comparisons.

Also, we should make sure our assignment statements have variables on the left side to assign to.

For instance, we’ll get this error is we write something like:

if (Math.PI = 3 || Math.PI = 4) {
  console.log('error');
}

Also, we should make sure that we have a variable on the left side of the assignment statement.

Therefore, we shouldn’t write statements like:

let  str = 'Hello, '
+= 'is it what '
+= 'you\'re looking for?';

To fix the first piece of code, we write:

if (Math.PI === 3 || Math.PI === 4) {
  console.log('error');
}

And to fix the 2nd piece of code, we write:

let str = "Hello, ";
str += "is it what ";
str += "you're looking for?";

Conclusion

To fix the ‘ReferenceError: invalid assignment left-hand side’ when we’re developing JavaScript apps, we should make sure that we’re accidentally using the = operator for doing comparisons.

Also, we should make sure our assignment statements have variables on the left side to assign to.