showing results for - "null coalescing on nested object js"
Valeria
24 May 2019
1// Have you used this?
2if (response.data && response.data.temperature) {
3  highTemperature = response.data.temperature.high;
4}
5
6// Optional chaining provides a terse alternative. 
7// This is JavaScript’s version of the 'safe navigation' operator
8
9const highTemperature = response.data?.temperature?.high;
10
11// In addition to accessing nested values, 
12// another common pattern in JavaScript is to use the logical OR operator (||) 
13// to coalesce values because it returns the first truthy operand, not a Boolean.
14const defaultTime = 2;
15const animationTime = settings.animationTime || defaultTime; 
16// if animationTime is set to 0 --> 0 is falsy, so will result in default time 2 instead of 0
17
18const animationTime = settings.animationTime ?? defaultTime;
19// ?? only rejects values if they are strictly null or undefined