CARVIEW |
Select Language
HTTP/2 307
date: Sun, 12 Oct 2025 02:35:42 GMT
content-type: text/html
content-length: 65
location: https://how.dev/answers/what-is-array-destructuring-in-javascript
cf-ray: 98d33491095975e9-BLR
cache-control: public, max-age=300, stale-while-revalidate=604800
referrer-policy: strict-origin-when-cross-origin
x-app-version: v251008-h-251010-1202
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-app-type: Learn
x-robots-tag: all
x-nextjs-cache: MISS
x-cloud-trace-context: a916da80fc661b7a493ab6def8f96377
via: 1.1 google
alt-svc: h3=":443"; ma=86400
cf-cache-status: EXPIRED
set-cookie: __cf_bm=Um_fjpJ6ZMAQ.DfSemZWpaWZQOSSvDxxZcuHp1d33ZQ-1760236542-1.0.1.1-1AD9SJ7Pzr12dDOFiAgPOPmNpKFKxwIeDL0olM5lXzWX_CCKXF9pqWXVK9wIrjy6SoUKUB1bljuc8tDD2yXL2Vz8nlkOP8nBD1C6Y.bygVY; path=/; expires=Sun, 12-Oct-25 03:05:42 GMT; domain=.educative.io; HttpOnly; Secure; SameSite=None
vary: Accept-Encoding
strict-transport-security: max-age=31536000; includeSubDomains; preload
server: cloudflare
HTTP/2 200
referrer-policy: strict-origin-when-cross-origin
x-app-version: v251008-h-251010-1202
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-app-type: Learn
x-middleware-rewrite: /answers/howdev/what-is-array-destructuring-in-javascript
x-nextjs-cache: MISS
x-cloud-trace-context: 0cdc46b93e94ae332d16bae43680b1c8
server: Google Frontend
via: 1.1 google
content-encoding: gzip
date: Sun, 12 Oct 2025 00:08:38 GMT
cache-control: public, max-age=300, stale-while-revalidate=604800
etag: W/"kbc1lotdkm6maf"
content-type: text/html; charset=utf-8
vary: Accept-Encoding
content-length: 57260
age: 8824
x-cache-status: stale
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
What is array destructuring in Javascript?
What is array destructuring in Javascript?
You can use array destructuring to extract individual values from an array and put them in new variables.
Code
let ages = [5, 10, 15, 20, 25],first, second, third, fourth, fifth, sixth;function agesFun(){return ages;}// Example 1[first, second, third] = ages;console.log(first, second, third); // 5, 10, 15// Example 2[first, second, ,fourth] = ages;console.log(first, second, fourth); // 5, 10, 20// Example 3[first, second, ...remaining] = ages;console.log(first, second, remaining); // 5, 10, [15, 20, 25]// Example 4[first, second, third, fourth, fifth, sixth] = ages;console.log(first, second, third, fourth, fifth, sixth); // 5, 10, 15, 20, 25, undefined// Example 5[first, second, third, fourth, fifth, sixth] = ages;console.log(first, second, third, fourth, fifth, sixth = 30); // 5, 10, 15, 20, 25, 30// Example 6[first, second, third, fourth, fifth] = agesFun();console.log(first, second, third, fourth, fifth); // 5, 10, 15, 20, 25
Explanation
- In Example 1,
first
,second
, andthird
variables are filled with5
,10
, and15
as values. - In Example 2,
first
,second
,fourth
variables are filled with5
,10
,20
as values. - In Example 3,
first
andsecond
variables are filled with5
and10
as values, and the rest of the values are filled in theremaining
variable[15, 20, 25]
as an array. - In Example 4, the
sixth
variable is set as undefined because array [5] does not exist. - In Example 5,
sixth
is set to30
. - In Example 6, the
agesFun
method returns the array sofirst
tofifth
variables are set to5
,10
,15
,20
and25
.
Relevant Answers
Explore Courses
Free Resources