array destructuring javascript w3schools code example

Example 1: destructure array javascript

var [first, second, ...rest] = ["Mercury", "Earth", ...planets, "Saturn"];

Example 2: 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);

Example 3: destructuring Array in JavaScript

let [greeting = "hi",name = "Sarah"] = ["hello"];

console.log(greeting);//"Hello"
console.log(name);//"Sarah"