Fireship · @fireship

? in NaN Seconds

Uploaded January 22, 2021 · Length 2:33

? in NaN Seconds

Summary

javascript question mark ternary operator optional chaining nullish coalescing explained in 100 seconds

  • javascript question mark
  • ternary operator
  • optional chaining
  • nullish coalescing

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:01

    the question mark it's one of the most

  2. 0:03

    powerful characters in the javascript

  3. 0:05

    universe providing syntactic sugar in

  4. 0:07

    three awesome yet totally unrelated ways

  5. 0:09

    first up we have the ternary operator

  6. 0:11

    the word ternary literally means

  7. 0:13

    composed of three parts

  8. 0:14

    using the question mark as our starting

  9. 0:16

    place we add a condition

  10. 0:18

    on the left side then on the right side

  11. 0:20

    we add a value to a sign

  12. 0:21

    when the condition is truthy but if the

  13. 0:23

    condition is falsy we add a colon to

  14. 0:25

    assign a different value the operator is

  15. 0:28

    often used to replace a traditional

  16. 0:30

    if statement the code in a ternary

  17. 0:32

    expression is far more concise

  18. 0:33

    but you might argue that it's much less

  19. 0:35

    readable than an if statement

  20. 0:37

    and you might be right that is until you

  21. 0:38

    need to assign a variable

  22. 0:40

    a statement like if is code that does

  23. 0:42

    something it's imperative

  24. 0:44

    it begins with state then as the

  25. 0:45

    program's executed it mutates the state

  26. 0:48

    and takes multiple lines of code just to

  27. 0:50

    assign a variable based on a simple

  28. 0:52

    condition

  29. 0:52

    the ternary operator on the other hand

  30. 0:54

    creates an expression which means that

  31. 0:56

    it produces a value

  32. 0:57

    that we can then assign to a variable

  33. 0:59

    this code is declarative

  34. 1:01

    it describes the state of the variable

  35. 1:02

    as opposed to mutating it that's why we

  36. 1:04

    can use const here but require

  37. 1:06

    let in the if statement declarative code

  38. 1:08

    is awesome because it can also be used

  39. 1:10

    in function arguments and in array and

  40. 1:12

    object literals but the question mark

  41. 1:14

    isn't done there in 2020

  42. 1:16

    an awesome new feature landed in

  43. 1:17

    javascript called optional chaining

  44. 1:19

    at some point you've likely written code

  45. 1:21

    that calls an object property that does

  46. 1:23

    not exist and when that happens it

  47. 1:25

    triggers an error

  48. 1:26

    at runtime you could write some code

  49. 1:27

    that asserts a property exists before

  50. 1:29

    calling it

  51. 1:30

    but this can get really ugly nowadays

  52. 1:32

    you can just insert a question mark

  53. 1:34

    between the property name and the period

  54. 1:36

    between the next property

  55. 1:37

    if the parent property is undefined

  56. 1:39

    it'll just return undefined instead of

  57. 1:41

    throwing an error

  58. 1:42

    that's a truly life-changing feature for

  59. 1:44

    the javascript developer

  60. 1:45

    but the question mark has one more trick

  61. 1:47

    up its sleeve nolish coalescing

  62. 1:49

    in many cases you may want to set a

  63. 1:51

    default value for a missing variable or

  64. 1:53

    property name

  65. 1:54

    you can easily do that with a logical or

  66. 1:56

    operator if the left side is falsy

  67. 1:58

    then use the value on the right side but