JavaScript Example (Program)
Learn Details

Array Destructuring


Javascript ES5 में Array की value को access करने के लिए array indexing का use किया जाता था। लेकिन Javascript ES6 में Array की value को accessकरने के लिए Array Destructuring का use किया गया है


Example 1 : ES5 Array



<script>    
   var std = [1, "Ram Kumar", "ram@gmail.com", 8788898899];
    var roll = std[0];
    var name = std[1];
    var email = std[2];
    var mob = std[3];
    document.write("Roll : "+ roll+"<br>");
    document.write("Name : "+ name+"<br>");
    document.write("E-mail : "+ email+"<br>");
    document.write("Mobile : "+ mob+"<br>");
</script>
 

Example 2 : Array Destructuring



<script>     
  let std = [1, "Ram Kumar", "ram@gmail.com", 8788898899];
    let [roll, name, email, mob] = std;
    document.write(`
      Roll : ${roll}<br> 
      Name : ${name}<br> 
      E-mail ${email}<br> 
      Mobile ${mob}
      `)
</script>
 

Example 3 : Array Destructuring undefined value



<script>     
    let std = [, , , ];
    let [roll, name, email, mob] = std;
    document.write(`
      Roll : ${roll}<br> 
      Name : ${name}<br> 
      E-mail ${email}<br> 
      Mobile ${mob}
      `)
</script>
 

Example 4 : set default value in array destruction



<script>     
    let std = [50, "Suresh Kumar" , , ];
    let [roll= 10, name= "Ramesh Kumar", email= "ramesh@gmail.com", mob=7778777989] = std;
    document.write(`
      Roll : ${roll}<br> 
      Name : ${name}<br> 
      E-mail ${email}<br> 
      Mobile ${mob}
      `);
</script>
 

Example 5 : Find specific value of array by destruction array



<script>     
    let std = [1, "Ram Kumar", "ram@gmail.com", 8788898899];
    let [, name, ,] = std;
    document.write(`
      Name : ${name}<br> 
  
      `)
</script>
 

Example 6 : Destructuring Array with inner Array



<script>     
    let std = [1, "Ram Kumar", "ram@gmail.com", 8788898899,["Developer", "3 Months"]];
    let [roll, name, email, mob,[course, duration]] = std;
    document.write(`
      Roll : ${roll}
Name : ${name}
E-mail : ${email}
Mobile : ${mob}
Course : ${course}
Course Duration : ${duration}
`) </script>

Example 7 : Destructuring Array with function



<script>     
    let myfunc = ([roll, name, email])=>{
        document.write(`
          Roll : ${roll}
Name : ${name}
E-mail : ${email}
`) } myfunc([1, "Ramesh Kumar", "ramesh@gmail.com"] </script>

Example 8 : Destructuring Array with return function



<script>     
   let myfunc = ()=>{
      return [1, "Ram Kumar", "ram@gmail.com", 8788898899]
    }
    let [roll, name, email, mob] = myfunc()

    document.write(`
        Roll : ${roll}
Name : ${name}
E-mail : ${email}
Mobile : ${mob}
`) </script>