MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/mb6zey/assigning_default_value/grzousk/?context=3
r/learnjavascript • u/golovatuy • Mar 23 '21
24 comments sorted by
View all comments
74
This isn’t quite correct, at least not anymore. This will also catch false and 0 since || will check for falsy values on the left hand side. You can use ?? nowadays to guard against only null and undefined
false
0
||
??
null
undefined
2 u/soaringradio Mar 23 '21 For anyone that is this concerned over data types, they should consider something safer like Typescript.
2
For anyone that is this concerned over data types, they should consider something safer like Typescript.
74
u/CarsonMcKinstry Mar 23 '21
This isn’t quite correct, at least not anymore. This will also catch
falseand0since||will check for falsy values on the left hand side. You can use??nowadays to guard against onlynullandundefined