Search code examples
javascripteslintdestructuringeslint-config-airbnb

Use/advantage of Array Destructing?


I have recently moved to 'airbnb-base' eslint style-guide. There i am get an error when i try to reference the array elements with index eslint(prefer-destructuring).

eg

let a = {};
// get an error saying eslint(prefer-destructuring)
a.b = clients[0];

What is wrong with using accessing Array with index or how is using array destructuring better ?


Solution

  • Here is what AirBnB has to say about the need for these rules.

    5.1 Use object destructuring when accessing and using multiple properties of an object. eslint: prefer-destructuring

    Why? Destructuring saves you from creating temporary references for those properties.

    5.3 Use object destructuring for multiple return values, not array destructuring.

    Why? You can add new properties over time or change the order of things without breaking call sites.

    The explanation for all the rules can be found in https://github.com/airbnb/javascript