Write clean code in JavaScript with this! 👇

Shehzad Hussain - May 16 - - Dev Community

Array destructuring. Here are 2 ways to do it:

const array = ["one", "two", "three"];
const [oneConst, twoConst, threeConst] = array;
oneConst = "one"
twoConst = "two"
threeConst = "three"

This way is the most commonly used.

I prefer it because it's more readable.

Here's the second way, which is not as widely used or known:

const array = ["one", "two", "three"];
const { 0: oneConst, 1: twoConst, 2: threeConst } = array;
oneConst = "one"
twoConst = "two"
threeConst = "three"

By using array destructuring, you will improve the readability and simplicity of your code.

You will have a reduced error-prone code because you don't need to manage indices everywhere, making indexing mistakes less likely.

Hope that's helpful.

. . . . . . . . . . . . . . . . . .