SyntaxError: Invalid destructuring assignment target in JS

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
2 min

banner

# SyntaxError: Invalid destructuring assignment target in JS

The "SyntaxError: Invalid destructuring assignment target" error occurs when we make a syntax error when declaring or destructuring an object, often in the arguments list of a function.

To solve the error make sure to correct any syntax errors in your code.

syntaxerror invalid destructuring assignment target

Here are some examples of how the error occurs:

index.js
// ⛔️ Uncaught SyntaxError: Invalid destructuring assignment target const obj = {name: 'Tom'}, {name: 'John'} // ------------------------------------------ // ⛔️ invalid destructuring assignment target function log({ key: "value"}) { console.log(arg) }; // ------------------------------------------ // // ⛔️ invalid destructuring assignment target const example = (['a','b']) => { console.log('a'); };

# Setting a variable to be equal to multiple objects

The first example throws the error because we set a variable to be equal to multiple objects without the objects being contained in an array.

index.js
// ⛔️ Uncaught SyntaxError: Invalid destructuring assignment target const obj = {name: 'Tom'}, {name: 'John'}

Instead, you should place the objects in an array or only assign a single value to the variable.

index.js
// ✅ declare an array of objects const arr = [{name: 'Tom'}, {name: 'John'}]; console.log(arr[0]); // 👉️ { name: 'Tom' } // -------------------------------------------- // ✅ declare a single object const obj = {id: 1, name: 'Bobby Hadz'}; console.log(obj['name']); // 👉️ Bobby hadz

declare array of objects or single object

The code for this article is available on GitHub

The square brackets [] syntax is used to declare an array and the curly braces {} syntax is used to declare an object.

Arrays are a collection of elements and objects are a mapping of key-value pairs.

# Incorrectly destructuring in a function's arguments

The second example throws the error because we incorrectly destructure an object in a function's arguments.

index.js
// ⛔️ SyntaxError: invalid destructuring assignment target function log({ key: "value"}) { console.log(arg) };

If you're trying to provide a default value for a function, use the equal sign instead.

index.js
// ✅ set a default value for a function's parameter function log(obj = {key: 'value'}) { console.log(obj); // 👉️ {key: 'value'} } log();

set default value for function parameter

The code for this article is available on GitHub

And you can destructure a value from an object argument as follows.

index.js
function log({key} = {key: 'value'}) { console.log(key); // 👉️ "value" } log();

destructuring value from object argument

Here's the same example but with arrays.

index.js
function log([first, second] = ['one', 'two']) { console.log(first, second); // 👉️ "one", "two" } log();
The code for this article is available on GitHub

If you can't figure out where exactly the error occurs, look at the error message in your browser's console or your terminal (if using Node.js).

syntaxerror invalid destructuring assignment target

The screenshot above shows that the error occurs on line 11 in the index.js file.

You can paste your code into an online Syntax Validator . The validator should be able to tell you on which line the error occurred.

You can also hover over the squiggly red line to get additional information.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.