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.

Categories
JavaScript Answers

How to Fix the ‘SyntaxError: missing = in const declaration’ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: missing = in const declaration’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘SyntaxError: missing = in const declaration’ when we’re developing JavaScript apps.

Fix the ‘SyntaxError: missing = in const declaration’ When Developing JavaScript Apps

To fix the ‘SyntaxError: missing = in const declaration’ when we’re developing JavaScript apps, we should make sure we assign a value to any variable that’s declared with const when they’re declared.

On Edge, the error message for this error is SyntaxError: Const must be initialized.

On Firefox, the error message for this error is SyntaxError: missing = in const declaration.

And on Chrome, the error message for this error is SyntaxError: Missing initializer in const declaration.

Therefore, the following code will throw the error:

const COLUMNS;

To fix this, we write:

const COLUMNS = 100;

However, we don’t have to assign a value to a variable declared with let when they’re declared, so:

let columns;

won’t throw the error.

Conclusion

To fix the ‘SyntaxError: missing = in const declaration’ when we’re developing JavaScript apps, we should make sure we assign a value to any variable that’s declared with const when they’re declared.

On Edge, the error message for this error is SyntaxError: Const must be initialized.

On Firefox, the error message for this error is SyntaxError: missing = in const declaration.

And on Chrome, the error message for this error is `SyntaxError: Missing initializer in const declaration.

Categories
JavaScript Answers

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

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.

Categories
JavaScript Answers

How to Fix the ‘SyntaxError: missing : after property id’ Error in Our JavaScript App?

Sometimes, we may run into the ‘SyntaxError: missing : after property id’ when we’re developing JavaScript apps.

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

Fix the ‘SyntaxError: missing : after property id’ When Developing JavaScript Apps

To fix the ‘SyntaxError: missing : after property id’ when we’re developing JavaScript apps, we should make sure we have a : separating object property key and value in our object literals.

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

And on Firefox, the error message for this error is SyntaxError: missing : after property id.

For instance, the following code will throw the error:

const obj = { propertyKey = 'value' };

since we have an = between propertyKey and 'value'.

To fix this, we write:

const obj = { propertyKey: 'value' };

We can also use the square bracket notation to add properties to objects:

So we can write:

const obj = { };
obj['propertyKey'] = 'value';

We can also write:

const obj = { ['b'+'ar']: 'foo' };

to add the bar property to obj with the square bracket notation.

The square bracket takes any expression that returns a string.

Conclusion

To fix the ‘SyntaxError: missing : after property id’ when we’re developing JavaScript apps, we should make sure we have a : separating object property key and value in our object literals.

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

And on Firefox, the error message for this error is SyntaxError: missing : after property id.