CARVIEW |
Select Language
HTTP/2 307
date: Sun, 12 Oct 2025 00:08:38 GMT
content-type: text/html
content-length: 65
location: https://how.dev/answers/what-is-array-destructuring-in-javascript
cf-ray: 98d25d230feac169-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: e5542f550cbaebef35706d0a8b35afb4;o=1
via: 1.1 google
alt-svc: h3=":443"; ma=86400
cf-cache-status: MISS
set-cookie: __cf_bm=IKwDQgorYKvI_fPPSPiAxr_w15z4fjxMYZ18kybYylc-1760227718-1.0.1.1-ZzEZqgyyyWrcBo0snSGWZ1U27LRGHrqm2BkOLhDz9I2C5e03NeC9YBwkh83sErzVye9fkAZMb3RcD0EFHi80Xj2PwV99SThtYMsuX.47ONM; path=/; expires=Sun, 12-Oct-25 00:38:38 GMT; domain=.educative.io; HttpOnly; Secure; SameSite=None
vary: Accept-Encoding
strict-transport-security: max-age=31536000; includeSubDomains; preload
server: cloudflare
HTTP/2 200
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-middleware-rewrite: /answers/howdev/what-is-array-destructuring-in-javascript
x-nextjs-cache: MISS
etag: W/"kbc1lotdkm6maf"
content-type: text/html; charset=utf-8
x-cloud-trace-context: 0cdc46b93e94ae332d16bae43680b1c8
date: Sun, 12 Oct 2025 00:08:38 GMT
server: Google Frontend
via: 1.1 google
vary: Accept-Encoding
content-encoding: gzip
x-cache-status: miss
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