Avoiding Errors with () and [] in Arrays

Modified on Wed, 31 Jul at 11:11 AM


Access array element

Array Destructuring

Incorrect way ( ❌ )

const numbers = [1, 2, 3, 4, 5];


numbers(0)

const numbers = [1, 2, 3, 4, 5];


const (first, second) = numbers

Correct way ( ✅ )

numbers[0]

const [first, second] = numbers


Table of Contents

  1. Confusing method invocation () with array access []

  2. Misusing [] in array destructuring


Confusing method invocation () with array access []

Can you figure out the issue in the below code:



Issue: 

push is invoked with parentheses (), not square brackets []



Fixed Code:

Methods are invoked using parentheses (), not square brackets []. Square brackets are used to access array elements.



Misusing [] in array destructuring

Can you figure out the issue in the below code:



Issue: 

Parentheses () are used instead of square brackets [] for array destructuring.



Fixed Code:

Array destructuring uses square brackets `[]` to match the structure of an array.




Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article