How to unpack array elements into separate variables using JavaScript ?
Given an array and the task is to unpack the value of array into separate variables by using preferred method using javascript.
Approach:
- Store the elements of an array into array variable.
- Now use ES6 script to declare a variable and unpack the value of array into separate variables.
Example 1: This example declare a variable using let keyword to unpack the array elements into separate variables.
<!DOCTYPE HTML> <html> <head> <title> How to unpack array elements into separate variables using JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var ar = [1, 2, 3, 4]; el_up.innerHTML = "Click on button to unpack the " + "values of array.<br> [" +ar + "]"; function GFG_Fun() { let [a, b, c, d] = ar; el_down.innerHTML = "Unpacked Values - " + a + ", " + b + ", " + c + ", " + d; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example declare a variable using var keyword to unpack the array elements into separate variables.
<!DOCTYPE HTML> <html> <head> <title> How to unpack array elements into separate variables using JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var ar = [1, 2, 3, 4]; el_up.innerHTML = "Click on button to unpack the" + " values of array.<br> [" +ar + "]"; function GFG_Fun() { var [a, b, c, d] = ar; el_down.innerHTML = "Unpacked Values - " + a + ", " + b + ", " + c + ", " + d; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- PHP | Separate odd and even elements from array without using loop
- How to get the elements of one array which are not present in another array using JavaScript?
- JavaScript | Add new elements at the beginning of an array
- Sum and Product of Array elements using JavaScript
- How to remove multiple elements from array in JavaScript ?
- JavaScript | Remove empty elements from an array
- How to pass JavaScript variables to PHP ?
- Variables and Datatypes in JavaScript
- How to unset JavaScript variables?
- How to find the index of all occurrence of elements in an array using JavaScript ?
- How to pass variables and data from PHP to JavaScript ?
- What are undeclared and undefined variables in JavaScript?
- Global and Local variables in JavaScript
- PHP | unpack() Function
- View the list of all variables in Google Chrome Console using JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



