Categories
JavaScript Answers

How to Fix the ‘SyntaxError: unterminated string literal’ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: unterminated string literal’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: unterminated string literal’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: unterminated string literal’ When Developing JavaScript Apps

To fix the ‘SyntaxError: unterminated string literal’ when we’re developing JavaScript apps, we should make sure that enclose our string content with single quotes, double quotes or backticks.

On Edge, the error message for this error is SyntaxError: Unterminated string constant.

And on Firefox, the error message for this error is SyntaxError: unterminated string literal.

For instance, if we’re trying to split a long string into multiple lines, we can’t write:

const longString = 'This is a very long string which needs
                  to wrap across multiple lines because
                  otherwise my code is unreadable.';

Instead, we write:

const longString = 'This is a very long string which needs \
                  to wrap across multiple lines because \
                  otherwise my code is unreadable.';

to add a backslash to separate a long string into multiple lines.

Conclusion

To fix the ‘SyntaxError: unterminated string literal’ when we’re developing JavaScript apps, we should make sure that enclose our string content with single quotes, double quotes or backticks.

On Edge, the error message for this error is SyntaxError: Unterminated string constant.

And on Firefox, the error message for this error is SyntaxError: unterminated string literal.

Categories
JavaScript Answers

How to Fix the ‘SyntaxError: test for equality (==) mistyped as assignment (=)? ‘ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: test for equality (==) mistyped as assignment (=)?’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: test for equality (==) mistyped as assignment (=)?’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: test for equality (==) mistyped as assignment (=)?’ When Developing JavaScript Apps

To fix the ‘SyntaxError: test for equality (==) mistyped as assignment (=)?’ when we’re developing JavaScript apps, we should make sure that we’re assigning variable values accidentally in the head of the if statement.

For instance, the following code will throw the error:

if (x = y) {
  // ...
}

since we have = instead of == or ===.

To fix this, we write:

if (x == y) {
  // ...
}

or

if (x === y) {
  // ...
}

Conclusion

To fix the ‘SyntaxError: test for equality (==) mistyped as assignment (=)?’ when we’re developing JavaScript apps, we should make sure that we’re assigning variable values accidentally in the head of the if statement.

Categories
JavaScript Answers

How to Fix the ‘SyntaxError: return not in function’ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: return not in function’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: return not in function’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: return not in function’ When Developing JavaScript Apps

To fix the ‘SyntaxError: return not in function’ when we’re developing JavaScript apps, we should make sure we’re using return and yield property inside a function.

On Edge, the error message for this error is SyntaxError: 'return' statement outside of function.

On Firefox, the error message for this error is SyntaxError: return not in function and SyntaxError: yield not in function .

For instance, if we have:

const cheer = function(score) {
  if (score === 147)
    return 'Maximum!';
  };
  if (score > 100) {
    return 'Century!';
  }
}

then we’ll get the error since the first if statement is missing the opening curly brace.

To fix it, we write:

const cheer = function (score) {
  if (score === 147) {
    return "Maximum!";
  }
  if (score > 100) {
    return "Century!";
  }
};

Conclusion

To fix the ‘SyntaxError: return not in function’ when we’re developing JavaScript apps, we should make sure we’re using return and yield property inside a function.

On Edge, the error message for this error is SyntaxError: 'return' statement outside of function.

On Firefox, the error message for this error is SyntaxError: return not in function and SyntaxError: yield not in function .

Categories
JavaScript Answers

How to Fix the ‘SyntaxError: missing formal parameter ‘ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: missing formal parameter’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: missing formal parameter’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: missing formal parameter’ When Developing JavaScript Apps

To fix the ‘SyntaxError: missing formal parameter’ when we’re developing JavaScript apps, we should make sure that our function signatures have expressions that are valid parameters.

For instance, the following code will all throw this error:

function square(3) {
  return number * number;
};

function greet("Howdy") {
  return greeting;
};

function log({ obj: "value"}) {
  console.log(arg)
};

The first function has a number instead of a variable as a parameter.

The 2nd function has a string literal instead of a variable as a parameter.

The last function has an object literal instead of a variable as a parameter.

To fix them, we write:

function square(number) {
  return number * number;
};

function greet(greeting) {
  return greeting;
};

function log(arg) {
  console.log(arg)
};

which all have variables as parameters.

Conclusion

To fix the ‘SyntaxError: missing formal parameter’ when we’re developing JavaScript apps, we should make sure that our function signatures have expressions that are valid parameters.

Categories
JavaScript Answers

How to Fix the ‘SyntaxError: missing ] after element list ‘ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: missing ] after element list’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: missing ] after element list’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: missing ] after element list’ When Developing JavaScript Apps

To fix the ‘SyntaxError: missing ] after element list’ when we’re developing JavaScript apps, we should make sure we have corresponding closing bracket for any opening bracket in our JavaScript code.

We should also make sure we separate entries that are supposed to be separated by a comma with a comma.

For instance, the following code will all throw the error:

let list = [1, 2,

let instruments = [
  'Ukulele',
  'Guitar',
  'Piano'
};

let data = [{foo: 'bar'} {bar: 'foo'}];

The first line is missing the closing bracket.

The 2nd array has a closing brace instead of a closing bracket.

And the last array is missing a comma in between the 2 objects.

To fix them, we write:

let list = [1, 2];

let instruments = ["Ukulele", "Guitar", "Piano"];

let data = [{ foo: "bar" }, { bar: "foo" }];

Conclusion

To fix the ‘SyntaxError: missing ] after element list’ when we’re developing JavaScript apps, we should make sure we have corresponding closing bracket for any opening bracket in our JavaScript code.

We should also make sure we separate entries that are supposed to be separated by a comma with a comma.