enum
todo: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-2.html
Optional Chaining Operator can help us reduce some code
when we judge foo.bar.baz
as follow:
// Beforeif (foo && foo.bar && foo.bar.baz) {// ...}// Afterif (foo?.bar?.baz) {// ...}
?:
: check in undefined
and null
.
undefined ?: 1 // undefinednull ?: 1 // undefined0 ?: 1 // 1'' ?: 1 // 1NaN ?: 1 // 1
&&
has different behaviour. Not onlyundefined
andnull
, butfalsy value
will be also checked. eg:
undefined && 1 // undefinednull && 1 // null0 && 1 // 0'' && 1 // ''NaN && 1 // NaN
??
means you can take the value that isn't undefined
or null
.
undefined ?? 1 // 1null ?? 1 // 10 ?? 1 // 0'' ?? 1 // ''
You can use it when using 0
as true such as:
if (typeof value !== 'number') return// beforelet valueStash = value === 0 || value ? value : defaultValue// afterlet valueStash = value ?? defaultValue
||
has different behaviour. Not onlyundefined
andnull
, butfalsy value
will be also checked. eg:
undefined || 1 // 1null || 1 // 10 || 1 // 1'' || 1 // 1