Fireship · @fireship
? in NaN Seconds
Uploaded January 22, 2021 · Length 2:33
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.
- 0:01
the question mark it's one of the most
- 0:03
powerful characters in the javascript
- 0:05
universe providing syntactic sugar in
- 0:07
three awesome yet totally unrelated ways
- 0:09
first up we have the ternary operator
- 0:11
the word ternary literally means
- 0:13
composed of three parts
- 0:14
using the question mark as our starting
- 0:16
place we add a condition
- 0:18
on the left side then on the right side
- 0:20
we add a value to a sign
- 0:21
when the condition is truthy but if the
- 0:23
condition is falsy we add a colon to
- 0:25
assign a different value the operator is
- 0:28
often used to replace a traditional
- 0:30
if statement the code in a ternary
- 0:32
expression is far more concise
- 0:33
but you might argue that it's much less
- 0:35
readable than an if statement
- 0:37
and you might be right that is until you
- 0:38
need to assign a variable
- 0:40
a statement like if is code that does
- 0:42
something it's imperative
- 0:44
it begins with state then as the
- 0:45
program's executed it mutates the state
- 0:48
and takes multiple lines of code just to
- 0:50
assign a variable based on a simple
- 0:52
condition
- 0:52
the ternary operator on the other hand
- 0:54
creates an expression which means that
- 0:56
it produces a value
- 0:57
that we can then assign to a variable
- 0:59
this code is declarative
- 1:01
it describes the state of the variable
- 1:02
as opposed to mutating it that's why we
- 1:04
can use const here but require
- 1:06
let in the if statement declarative code
- 1:08
is awesome because it can also be used
- 1:10
in function arguments and in array and
- 1:12
object literals but the question mark
- 1:14
isn't done there in 2020
- 1:16
an awesome new feature landed in
- 1:17
javascript called optional chaining
- 1:19
at some point you've likely written code
- 1:21
that calls an object property that does
- 1:23
not exist and when that happens it
- 1:25
triggers an error
- 1:26
at runtime you could write some code
- 1:27
that asserts a property exists before
- 1:29
calling it
- 1:30
but this can get really ugly nowadays
- 1:32
you can just insert a question mark
- 1:34
between the property name and the period
- 1:36
between the next property
- 1:37
if the parent property is undefined
- 1:39
it'll just return undefined instead of
- 1:41
throwing an error
- 1:42
that's a truly life-changing feature for
- 1:44
the javascript developer
- 1:45
but the question mark has one more trick
- 1:47
up its sleeve nolish coalescing
- 1:49
in many cases you may want to set a
- 1:51
default value for a missing variable or
- 1:53
property name
- 1:54
you can easily do that with a logical or
- 1:56
operator if the left side is falsy
- 1:58
then use the value on the right side but


