How to unpack array elements into separate variables using JavaScript ? Last Updated : 23 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given an array and the task is to unpack the value of the array into separate variables by using the preferred method using javascript.We can unpack the array elements into separate variables by the following methods:Table of ContentDestructuring AssignmentUsing Array.slice() MethodUsing the spread operatorUsing Array.prototype.shift()Approach 1: Destructuring AssignmentStore the elements of an array into an array variable.Now use the ES6 script to declare a variable and unpack the value of the array into separate variables.Example 1: This example declares a variable using the let keyword to unpack the array elements into separate variables. JavaScript const arr = [1, 2, 3, 4]; function GFG_Fun() { let [a, b, c, d] = arr; console.log(a); console.log(b); console.log(c); console.log(d); } GFG_Fun(); Output1 2 3 4Approach 2: Using Array.slice() MethodThe Javascript arr.slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged.Example: JavaScript const array = [1, 2, 3, 4]; function GFG_Fun() { const a = array.slice(0, 1)[0]; const b = array.slice(1, 2)[0]; const c = array.slice(2, 3)[0]; console.log(a); console.log(b); console.log(c); } GFG_Fun(); Output1 2 3Approach 3: Using the spread operatorThe Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array. Example: JavaScript const array = [1, 2, 3]; const [a, b, c] = [...array]; console.log(a); console.log(b); console.log(c); Output1 2 3Approach 4: Using Array.prototype.shift()The shift() method removes the first element from an array and returns that element. This method modifies the original array.Example: To demonstrate unpacking array elements into separate variables using shift(). JavaScript const array = [1, 2, 3, 4]; function GFG_Fun() { const a = array.shift(); const b = array.shift(); const c = array.shift(); const d = array.shift(); console.log(a); console.log(b); console.log(c); console.log(d); } GFG_Fun(); Output1 2 3 4 Comment More infoAdvertise with us Next Article How to unpack array elements into separate variables using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to get Values from HTML Input Array using JavaScript? This problem can be solved by using input tags having the same "name" attribute value that can group multiple values stored under one name which could later be accessed by using that name. Syntaxlet input = document.getElementsByName('array[]');What is an Input Array in HTML?An "input array" in HTML 2 min read How to convert a 2D array to a comma-separated values (CSV) string in JavaScript ? Given a 2D array, we have to convert it to a comma-separated values (CSV) string using JS. Input:[ [ "a" , "b"] , [ "c" ,"d" ] ]Output:"a,b c,d"Input:[ [ "1", "2"]["3", "4"]["5", "6"] ]Output:"1,23,45,6"To achieve this, we must know some array prototype functions which will be helpful in this regard 4 min read How to Merge/Combine Arrays using JavaScript? Given two or more arrays, the task is to merge (or combine) arrays to make a single array in JavaScript. The simplest method to merge two or more arrays is by using array.concat() method.Using Array concat() MethodThe contact() method concatenates (joins) two or more arrays. It creates a new array a 2 min read How to map array values without using map method in JavaScript ? Array elements can be mapped by using looping methods in JavaScript. The map() method creates a new array with the results of the output of a function called for each array element. This can also be implemented using a for loop in JavaScript.Approach 1: For this, we can create two arrays, in which o 3 min read How to Split JavaScript Array in Chunks using Lodash? Splitting a JavaScript array into chunks consists of dividing the array into smaller subarrays based on a specified size or condition. In this article, we will explore different approaches to splitting JavaScript array into chunks using Lodash. Below are the approaches to Split JavaScript array in c 2 min read How to Declare Multiple Variables in JavaScript? JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.Decla 2 min read How to store a key=> value array in JavaScript ? In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value. This allows you to store and retrieve data using named keys instead of numeric indices, enabling more intuitive access to the stored information.Here are some common approaches:Table 4 min read JavaScript - Convert Comma Separated String To Array Here are the various methods to convert comma-separated string to array using JavaScript.1. Using the split() Method (Most Common)The split() method is the simplest and most commonly used way to convert a comma-separated string into an array. It splits a string into an array based on a specified cha 3 min read How to convert Integer array to String array using JavaScript ? The task is to convert an integer array to a string array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. Approaches to convert Integer array to String array:Table of ContentApproach 1: using JavaScript array.map() and toString() methodsApproach 2: Us 2 min read Different Ways to Use Array Slice in JavaScript In this article, we will see the different ways to use the Array slice method in Javascript. Using Array Slice in JavaScript refers to the technique of extracting a specified portion of an array, defined by start and end indices, to create a new array containing those selected elements. Syntaxarr.sl 4 min read Like