Destructing Array pada ES6

Run Settings
LanguageJavascript
Language Version
Run Command
console.log(`============================= Destructing Array sebelum ES6 =============================`); const food = ['Jambu', 'Belimbing', 'Manggis'] const myFood = food[0] const yourFood = food[1] const ourFood = food[2] console.log(myFood, yourFood, ourFood) /* Destructuring Array serupa dengan destructuring object. Jika objek menggunakan tanda kurung kurawal { } sedangkan array menggunakan tanda kurung siku [ ]. */ console.log(` ============================= Destructing Array sesudah ES6 =============================`); const favorites = ['Ayam Kentucky', 'Ayam Goreng', 'Mie Telor', 'Kulit ayam goreng']; const [food_0, food_1, food_2, food_3] = favorites; const [, , thirdFood, ] = favorites; console.log(`1. ${food_0}\n2. ${food_1}\n3. ${food_2}\n4. ${food_3}`) /* jika kita ingin mengambil nilai ketiga dari array kita bisa melakukannya dengan membiarkan index array yang kita tidak inginkan tetap kosong. Lebih lanjut, tanda koma tetap (,) tetap diperlukan untuk menunjukkan posisi index-nya seperti ini: */ console.log(thirdFood); console.log(` =============================== Destructing Assignment pd Array ===============================`); const hobby = ['Basket', 'Motocross', 'cook']; let myHobby = "Computer"; let herHobby = "Swimming"; console.log(myHobby, herHobby); [myHobby, herHobby] = favorites; console.log("After destructing Assignment: " + myHobby, herHobby); console.log(` Menukar nilai dgn Array Destructing Assignment (before ES6)`) var a = 1; var b = 2; var temp; console.log(`Sebelum Swap : Nilai a: ${a} Nilai b: ${b}`); temp = a; a = b; b = temp; console.log(` Setelah Swap : Nilai a: ${a} Nilai b: ${b}`); console.log(` Menukar nilai dgn Array Destructing Assignment (after ES6)`) let A = 1; let B = 2; let C = 3; let D = 4; console.log(`Sebelum Swap : Nilai A: ${A} Nilai B: ${B} Nilai C: ${C} Nilai D: ${D}`); [A, B, C, D] = [B, C, D, A]; console.log(`Sesudah Swap : Nilai A: ${A} Nilai B: ${B} Nilai C: ${C} Nilai D: ${D}`);
Editor Settings
Theme
Key bindings
Full width
Lines