how to destructure an array inside array js code example

Example 1: array destructuring

//Without destructuring

const myArray = ["a", "b", "c"];

const x = myArray[0];
const y = myArray[1];

console.log(x, y); // "a" "b"

//With destructuring

const myArray = ["a", "b", "c"];

const [x, y] = myArray; // That's it !

console.log(x, y); // "a" "b"

Example 2: javascript nested array destructuring

let arr = [1, 2, 3, 4, [100, 200, 300], 5, 6, 7];

// nested array destructuring
const [a, , , , [, b, ,], , , ,] = arr;

console.log(a);
// expected output "1"

console.log(b);
// expected output "200"

Example 3: array destructuring in javascript

const array = ['ismail', 'sulman'];
// array destructuring
const [firstElement, secondElement] = array;
console.log(firstElement, secondElement);
const secondArray = ['ismail', 'naeem', 'Mr', 1];
const [firstEl, secondEl, ...array1] = secondArray;
console.log(firstEl, secondEl, array1);