Categories
JavaScript Answers

How to Fix the ‘SyntaxError: missing ; before statement ‘ Error in Our JavaScript App?

Spread the love

Sometimes, we may run into the ‘SyntaxError: missing ; before statement’ when we’re developing JavaScript apps.

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

Fix the ‘SyntaxError: missing ; before statement’ When Developing JavaScript Apps

To fix the ‘SyntaxError: missing ; before statement’ when we’re developing JavaScript apps, we should make sure we have semicolons in places where the JavaScript engine can’t insert a semicolon with automatic semicolon insertion.

On Edge, the error message for this error is SyntaxError: Expected ';'.

On Firefox, the error message for this error is SyntaxError: missing ; before statement.

For instance, if we have:

const foo = 'Jane's bar';

then we’ll get this error since there’s an unescaped single quote in between the single quote string delimiters.

To fix this, we write:

const foo = "Jane's bar";

or

const foo = 'Jane\'s bar';

which are both valid since the first string uses double quotes to wrap the string, which doesn’t conflict with the single quote in the string.

The 2nd string escaped the single quote inside the string, so it’s also valid.

We also can’t declare properties of an object or array with any keyword.

So instead of writing:

let obj = {};
let  obj.foo = 'hi';

or

let array = [];
let array[0] = 'there';

We write:

let obj = {};
obj.foo = 'hi';

or

let array = [];
array[0] = 'there';

Conclusion

To fix the ‘SyntaxError: missing ; before statement’ when we’re developing JavaScript apps, we should make sure we have semicolons in places where the JavaScript engine can’t insert a semicolon with automatic semicolon insertion.

On Edge, the error message for this error is SyntaxError: Expected ';'.

On Firefox, the error message for this error is SyntaxError: missing ; before statement.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *