Fireship · @fireship
JS Destructuring in 100 Seconds
Uploaded January 12, 2021 · Length 3:31
Summary
Learn how to use destructuring assignment in JavaScript to make code more concise and readable.
- javascript destructuring
- array destructuring
- object destructuring
- javascript syntax
- code optimization
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
destructuring assignment take values
- 0:02
from arrays or properties from objects
- 0:04
and set them
- 0:05
as local variables it's a technique that
- 0:07
can make your javascript code more
- 0:09
concise and readable
- 0:10
imagine you have an array and you want
- 0:12
to assign some of its values to local
- 0:14
variables
- 0:15
one option is to assign each variable by
- 0:17
referencing its index in the array
- 0:19
that works fine but it's not very
- 0:21
elegant with destructuring we can do the
- 0:23
same thing with a single line of code
- 0:25
by using brackets after const we can
- 0:27
assign a variable name
- 0:28
to each index in the array the position
- 0:30
of the variable name
- 0:31
matches the values index in the array
- 0:33
the syntax may look familiar to you if
- 0:35
you've ever used react which relies on
- 0:37
destructuring for the used state hook
- 0:39
when working with arrays you may want to
- 0:40
emit a variable which you can do by
- 0:42
adding a comma without a variable name
- 0:44
to skip that index
- 0:46
in other cases you might want to name
- 0:47
the first couple variables
- 0:49
then put the remainder in their own
- 0:50
array that can be achieved by putting
- 0:52
three dots in front of the variable name
- 0:54
you might also want to set a default
- 0:55
value in case the value in the array is
- 0:58
undefined use the equal sign to provide
- 1:00
the fallback value
- 1:01
in addition to arrays destructuring also
- 1:03
applies to objects
- 1:05
notice how we're duplicating the
- 1:06
object's property name as a variable
- 1:08
with dot notation
- 1:09
by putting braces after const you can
- 1:11
reference property names which will
- 1:13
automatically become local variables
- 1:15
which is much cleaner than referencing
- 1:17
them line by line with dot notation
- 1:19
and just like arrays you can use the
- 1:20
equal sign to set a default value
- 1:23
but in some cases you may want to use a
- 1:24
different name than what's provided on
- 1:26
the object itself
- 1:27
adding a colon after the property name
- 1:29
allows you to rename it which is
- 1:31
especially useful when dealing with name
- 1:33
collisions or when working with objects
- 1:35
that use property names that are not
- 1:37
valid variable names
- 1:38
in addition to renaming you can also use
- 1:40
a colon to access
- 1:41
nested properties or in other words set
- 1:44
a variable from an object within an
- 1:45
object
- 1:46
in addition to destructuring top level
- 1:48
variables you can also use the syntax in
- 1:50
for loops
- 1:51
which is awesome when you have an array
- 1:52
of objects when working with functions
- 1:55
you can destructure arrays and objects
- 1:57
passed as arguments
- 1:58
providing yet another way to obliterate


