Fireship · @fireship

JS Destructuring in 100 Seconds

Uploaded January 12, 2021 · Length 3:31

JS Destructuring in 100 Seconds

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.

  1. 0:00

    destructuring assignment take values

  2. 0:02

    from arrays or properties from objects

  3. 0:04

    and set them

  4. 0:05

    as local variables it's a technique that

  5. 0:07

    can make your javascript code more

  6. 0:09

    concise and readable

  7. 0:10

    imagine you have an array and you want

  8. 0:12

    to assign some of its values to local

  9. 0:14

    variables

  10. 0:15

    one option is to assign each variable by

  11. 0:17

    referencing its index in the array

  12. 0:19

    that works fine but it's not very

  13. 0:21

    elegant with destructuring we can do the

  14. 0:23

    same thing with a single line of code

  15. 0:25

    by using brackets after const we can

  16. 0:27

    assign a variable name

  17. 0:28

    to each index in the array the position

  18. 0:30

    of the variable name

  19. 0:31

    matches the values index in the array

  20. 0:33

    the syntax may look familiar to you if

  21. 0:35

    you've ever used react which relies on

  22. 0:37

    destructuring for the used state hook

  23. 0:39

    when working with arrays you may want to

  24. 0:40

    emit a variable which you can do by

  25. 0:42

    adding a comma without a variable name

  26. 0:44

    to skip that index

  27. 0:46

    in other cases you might want to name

  28. 0:47

    the first couple variables

  29. 0:49

    then put the remainder in their own

  30. 0:50

    array that can be achieved by putting

  31. 0:52

    three dots in front of the variable name

  32. 0:54

    you might also want to set a default

  33. 0:55

    value in case the value in the array is

  34. 0:58

    undefined use the equal sign to provide

  35. 1:00

    the fallback value

  36. 1:01

    in addition to arrays destructuring also

  37. 1:03

    applies to objects

  38. 1:05

    notice how we're duplicating the

  39. 1:06

    object's property name as a variable

  40. 1:08

    with dot notation

  41. 1:09

    by putting braces after const you can

  42. 1:11

    reference property names which will

  43. 1:13

    automatically become local variables

  44. 1:15

    which is much cleaner than referencing

  45. 1:17

    them line by line with dot notation

  46. 1:19

    and just like arrays you can use the

  47. 1:20

    equal sign to set a default value

  48. 1:23

    but in some cases you may want to use a

  49. 1:24

    different name than what's provided on

  50. 1:26

    the object itself

  51. 1:27

    adding a colon after the property name

  52. 1:29

    allows you to rename it which is

  53. 1:31

    especially useful when dealing with name

  54. 1:33

    collisions or when working with objects

  55. 1:35

    that use property names that are not

  56. 1:37

    valid variable names

  57. 1:38

    in addition to renaming you can also use

  58. 1:40

    a colon to access

  59. 1:41

    nested properties or in other words set

  60. 1:44

    a variable from an object within an

  61. 1:45

    object

  62. 1:46

    in addition to destructuring top level

  63. 1:48

    variables you can also use the syntax in

  64. 1:50

    for loops

  65. 1:51

    which is awesome when you have an array

  66. 1:52

    of objects when working with functions

  67. 1:55

    you can destructure arrays and objects

  68. 1:57

    passed as arguments

  69. 1:58

    providing yet another way to obliterate